1 import requests
2 import json
3
4
5 class BaiduTranslateSpider:
6 def __init__(self, kw):
7 self.url = "https://fanyi.baidu.com/sug"
8 self.headers = {
9 "User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36",
10 # "Accept": "*/*",
11 # "Accept-Encoding": "gzip,deflate,br",
12 # "Accept-Language": "zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7",
13 # "Connection": "keep-alive",
14 # "Content-Length":"96",
15 # "Content-Type": "application/x-www-form-urlencoded",
16 # "Host":"fanyi.baidu.com",
17 # "X-Requested-With":"XMLHttpRequest",
18 # "Cookie":"BAIDUID=B9EF168607A445F55A1255F6A19E71A8:FG=1; BDUSS=2NVd0g3VzJiMzdId1EtSlRHMlA2blo2Q1JpOUJLejVzdHdlTEI1YkFyQktpS0ZjQUFBQUFBJCQAAAAAAAAAAAEAAAD3nazpbWlhbmZlaXZpcDU1NgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEr7eVxK-3lca; BIDUPSID=B9EF168607A445F55A1255F6A19E71A8; PSTM=1551499451; delPer=0; PSINO=1; H_PS_PSSID=1464_21108_28585_26350_28604_20719; locale=zh; Hm_lvt_64ecd82404c51e03dc91cb9e8c025574=1551677300; REALTIME_TRANS_SWITCH=1; FANYI_WORD_SWITCH=1; HISTORY_SWITCH=1; SOUND_SPD_SWITCH=1; SOUND_PREFER_SWITCH=1; from_lang_often=%5B%7B%22value%22%3A%22en%22%2C%22text%22%3A%22%u82F1%u8BED%22%7D%2C%7B%22value%22%3A%22zh%22%2C%22text%22%3A%22%u4E2D%u6587%22%7D%5D; to_lang_often=%5B%7B%22value%22%3A%22zh%22%2C%22text%22%3A%22%u4E2D%u6587%22%7D%2C%7B%22value%22%3A%22en%22%2C%22text%22%3A%22%u82F1%u8BED%22%7D%5D; Hm_lvt_afd111fa62852d1f37001d1f980b6800=1551678783; Hm_lpvt_afd111fa62852d1f37001d1f980b6800=1551678783; Hm_lpvt_64ecd82404c51e03dc91cb9e8c025574=1551678783",
19 # "Origin":"https://fanyi.baidu.com",
20 # "Referer":"https://fanyi.baidu.com/"
21 }
22 self.data = {
23 "kw": kw,
24 }
25
26 def run(self):
27 response = requests.post(self.url, headers=self.headers, data=self.data)
28 if response.status_code == 200:
29 # 处理返回的json格式字符串
30 print(response.content.decode())
31 content = json.loads(response.content.decode()) # response.json()
32 print(content)
33 result = content["data"][0]["v"]
34 print(result)
35
36
37 def main():
38 spider = BaiduTranslateSpider("人生苦短")
39 spider.run()
40
41
42 if __name__ == '__main__':
43 main()
没法实现较长的词汇翻译,大概只能4个单词,超过就请求失败