参数处理步骤
post请求的参数我们不会拼接到url的后面,所以参数一般都会放到请求对象定制的参数中
- 先进行编码,一般要编码成
utf-8
data = urllib.parse.urlencode(data).encode('utf-8')
- 定制请求对象
request = urllib.request.Request(url=url,data=data,headers=headers)
- 发送请求
response = urllib.request.urlopen(request)
例子
打开百度翻译,我们会发现他执行查询的api如下图
我么可以通过图片信息定制一下我们的请求参数,然后发送请求
import urllib.request
import urllib.parse
import json
url = 'https://fanyi.baidu.com/v2transapi?from=en&to=zh'
headers = {
"Cookie": "BIDUPSID=5FCCDA14B204CB4CC2D616DA995A7A7B; PSTM=1626058547; __yjs_duid=1_8ea4d762d95c93464b59dc1ba62bf2361626061853782; MCITY=288-288%3A; REALTIME_TRANS_SWITCH=1; HISTORY_SWITCH=1; FANYI_WORD_SWITCH=1; SOUND_SPD_SWITCH=1; SOUND_PREFER_SWITCH=1; BAIDUID=3D08497B0FE78CF6EE65C6842A4C0118:FG=1; BDUSS=ZoSDFxeXIybG9uLXlmRnRDWlo4ekdWcktrRjktVDFMMXh5cENQWGFUaVZQRlJpRVFBQUFBJCQAAAAAAAAAAAEAAACyovVKwLLAsrfFxvoxMDAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAJWvLGKVryxiRk; BDUSS_BFESS=ZoSDFxeXIybG9uLXlmRnRDWlo4ekdWcktrRjktVDFMMXh5cENQWGFUaVZQRlJpRVFBQUFBJCQAAAAAAAAAAAEAAACyovVKwLLAsrfFxvoxMDAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAJWvLGKVryxiRk; BDORZ=FFFB88E999055A3F8A630C64834BD6D0; H_PS_PSSID=31253_26350; BDRCVFR[tiixOo0cjw_]=mk3SLVN4HKm; delPer=0; PSINO=5; BAIDUID_BFESS=14E1F463C8A7BC61577793AEF644C2DE:FG=1; BA_HECTOR=8h04ak048g80800lco1h4qagu0r; Hm_lvt_64ecd82404c51e03dc91cb9e8c025574=1649224224; Hm_lpvt_64ecd82404c51e03dc91cb9e8c025574=1649224224; APPGUIDE_10_0_2=1; ab_sr=1.0.1_NzI2MzNkMmIzZmY0MzUzODE2NDVmMGYxNGJmMDY3YmMzZDkyZmIzNmJhZTc2NDkzZmEzNjM1YjU0MjVkMWRhZTA2YmViZjJlZTVlYmZkZWI3NzNiODQzMDc1MTE3ZGNiMTQ5N2VmZWI2YmE1ZWVjYmM0MzYyNjhlM2JjMmM1NjkzZGNlYmQwZmJjYjMxNGM0NzNkNDg1NzgxYmU5OTYzNjFiN2IzYjg4ODUzMTRjYjBmNmU4NGQ3NzA4YjEwYjI1"
}
data = {
'from': 'zh',
'to': 'en',
'query': '执行器',
'transtype': 'realtime',
'simple_means_flag': '3',
'sign': '887604.616453',
'token': '78ed6b12e598c5f110045b72215bbea9',
'domain': 'common',
}
data = urllib.parse.urlencode(data).encode('utf-8')
request = urllib.request.Request(url = url,data = data,headers = headers)
response = urllib.request.urlopen(request)
content = response.read().decode('utf-8')
obj = json.loads(content)
print(obj["trans_result"]["data"][0]["result"])
|