一、导入模块

import urllib.request
import urllib.parse
import json

二、指定url,构造headers,构造请求参数

  • 1.指定url
url = 'http://fanyi.baidu.com/v2transapi'
  • 2.构造headers
headers = {
    'Host': 'fanyi.baidu.com',
    'Connection': 'keep-alive',
    # 'Content-Length': '7',  不能固定长度
    '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-Encoding': 'gzip, deflate', #不接受压缩文件
    '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)

  • 1.将json格式的字符串转化为python对象
obj = json.loads(result)
  • 2.将python对象转为json格式字符串,注意,要添加第二个参数,禁用ascii编码
string = json.dumps(obj,ensure_ascii=False)
  • 3.将字符串以utf-8格式写入文件
with open('wolf.json','w',encoding='utf-8') as f1:
    f1.write(string)
GitHub 加速计划 / js / json
41.72 K
6.61 K
下载
适用于现代 C++ 的 JSON。
最近提交(Master分支:1 个月前 )
960b763e 4 个月前
8c391e04 6 个月前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐