1:安装requests模块
pip install requests
2:打开百度翻译进行抓包,刚刚操作了一波发现pc版的正面不好刚,有sign不知道是什么玩意,,emmmm,刚了半天不知道,所有切换到手机版,奇迹出现了,,,嘿嘿嘿....
3:抓取接口
Request URL:
https://fanyi.baidu.com/basetrans
4:python的代码
import requests
import json
url = "https://fanyi.baidu.com/basetrans"
headers_s = {
"User-Agent": "Mozilla/5.0 (Linux; Android 5.0; SM-G900P Build/LRX21T) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Mobile Safari/537.36"
}
data_s = {
"query": "你好",
"from": "zh",
"to": "en"
}
r = requests.post(url, data=data_s, headers=headers_s)
# print(r.content.decode())
res = json.loads(r.content.decode())
print(res['trans'][0]['dst'])
解析成json需要导入json模块,最后效果如下: