一、导入模块
import urllib.request
import urllib.parse
import json
url = 'http://fanyi.baidu.com/v2transapi'
headers = {
'Host': 'fanyi.baidu.com',
'Connection': 'keep-alive',
'Accept': 'application/json, text/javascript, */*; q=0.01',
'Origin': 'http://fanyi.baidu.com',
'X-Requested-With': 'XMLHttpRequest',
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.108 Safari/537.36',
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'Referer': 'http://fanyi.baidu.com/translate',
'Accept-Language': 'zh-CN,zh;q=0.9',
'Cookie': 'BIDUPSID=9F5816DD3088F4291EA4C12FFC2ABCDE; BAIDUID=78AF1CE91C8F84BD601F6E2778C618DA:FG=1; PSTM=1513827071; FP_UID=49cb193525cef45caf0af69c1e64ccf6; BDRCVFR[DDAxsax1-qs]=mk3SLVN4HKm; H_PS_PSSID=1454_21125_25177; PSINO=2; BDORZ=FFFB88E999055A3F8A630C64834BD6D0; locale=zh; Hm_lvt_64ecd82404c51e03dc91cb9e8c025574=1514168525; Hm_lpvt_64ecd82404c51e03dc91cb9e8c025574=1514168525; to_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; 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%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',
}
- 3.构造请求参数(参数格式与真实参数一致,可通过抓包工具观察)
word = 'wolf'
data = {
'from': 'en',
'to': 'zh',
'query': word,
'transtype': 'realtime',
'simple_means_flag': '3',
}
data = urllib.parse.urlencode(data).encode('utf-8')
三、模拟请求
request = urllib.request.Request(url=url,headers=headers,data=data)
四、发送请求
reponse = urllib.request.urlopen(request)
五、获取结果(结果显示,获取的数据为assii编码)
result = reponse.read().decode('utf-8')
六、存储数据结果(需要将assii编码转化为utf-8)
obj = json.loads(result)
- 2.将python对象转为json格式字符串,注意,要添加第二个参数,禁用ascii编码
string = json.dumps(obj,ensure_ascii=False)
with open('wolf.json','w',encoding='utf-8') as f1:
f1.write(string)