Python 之有道翻译数据抓取
import requests import time def you_dao(): key = input("请输入要翻译的内容:") # key = "哈哈" # 构建url链接 # url = 'http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule' # 这里要去掉?号前面的_o,不然会进行加密算法,导致失败 url = 'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule' salt = int(time.time() * 10000) ts = int(salt / 10) form_data = { "i": key, "from": "AUTO", "to": "AUTO", "smartresult": "dict", "client": "fanyideskweb", "salt": salt, "sign": "abf857d70c24cb55263b1f624193b38b", "ts": ts, "bv": "bbb3ed55971873051bc2ff740579bb49", "doctype": "json", "version": "2.1", "keyfrom": "fanyi.web", "action": "FY_BY_REALTlME", } headers = { "User-Agent": 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36' } json_data = requests.get(url, params=form_data, headers=headers).json() print(json_data['translateResult']) return json_data['translateResult'][0][0]['tgt'] if __name__ == '__main__': you_dao()
运行效果如图: