有道翻譯spider簡易版
阿新 • • 發佈:2018-12-12
import requests import json class SpiderYouDao(object): def __init__(self, sentence): self.url = "http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule" self.sentence = sentence self.headers = {"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36"} def get_data(self, content): data = { 'i': content, 'from': 'AUTO', 'to': 'AUTO', 'smartresult': 'dict', 'client': 'fanyideskweb', 'salt': '15438901316318', 'sign': '2ab7f2f64515f0004f89e3104fa74322', 'ts': '1543890131631', 'bv': '6728b93ea32700f1d7e56b2bead018b5', 'doctype': 'json', 'version': '2.1', 'keyfrom': 'fanyi.web', 'action': 'FY_BY_CLICKBUTTION', 'typoResult': 'false', } return data def parse_url(self, data): """傳送請求,獲取請求資料""" response = requests.post(self.url, data=data, headers=self.headers) res = (response.content.decode("utf-8")) return res def get_ret(self, json_str): sen_dict = json.loads(json_str) return sen_dict['translateResult'][0][0]["tgt"] def run(self, content): # 1、獲取url地址,相關引數, post_data = self.get_data(content) # 3、獲取相應內容 response = self.parse_url(post_data) # 提取資料 sen = self.get_ret(response) print(sen) if __name__ == '__main__': a = SpiderYouDao('') a.run("")