一、POST请求
以百度翻译为例,打开F12,选择All。在输入单词spider的同时会加载出来许多接口,难点之一,我们要找到我们需要的接口。我们需要找的是sug接口,找到最后一个sug接口(因为我们是一个字母一个字母输入的,所以有多个sug),复制它的url。 在这个sug接口的preview中确实有我们需要的翻译信息,证明这就是我们需要的接口。在payload中能获取最重要的参数,这里的kw也就是我们需要翻译的内容。
urllib的post请求和之前的get请求有一些区别
- post请求的参数必须要进行编码( encode() ),不能是简单的str类型
- post请求的参数需要作为参数(data=)放在定制的请求对象中,不能简单的进行url拼接
完整代码:
import urllib.request
import urllib.parse
url = 'https://fanyi.baidu.com/sug'
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/98.0.4758.102 Safari/537.36 "
}
data = {
'kw': 'spider'
}
data = urllib.parse.urlencode(data).encode('utf-8')
request = urllib.request.Request(url=url, data=data, headers=headers)
res = urllib.request.urlopen(request)
content = res.read().decode('utf-8')
print(content)
打印结果是一个字符串类型,里面有许多看不懂的内容。观察能看出这其实是一个字典(老师这里说是json)类型,所以需要反序列化才能看懂。Python 序列化和反序列化
最终代码和结果:
import urllib.request
import urllib.parse
import json
url = 'https://fanyi.baidu.com/sug'
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/98.0.4758.102 Safari/537.36 "
}
data = {
'kw': 'spider'
}
data = urllib.parse.urlencode(data).encode('utf-8')
request = urllib.request.Request(url=url, data=data, headers=headers)
res = urllib.request.urlopen(request)
content = res.read().decode('utf-8')
content = json.loads(content)
print(content)
二、POST请求进阶案例
还是刚才spider的翻译,这次是获取详细翻译
详细翻译的接口在v2transapi的接口中,第一次找需要花费一段时间。。也是要找最后一个v2transapi 还是要从payload中的Form Data获取关键参数
同样地代码,结果会显示‘未知错误’,如图 是因为遭遇了反爬,解决方案是把接口中的headers请求头全都复制过来。 个人建议先把信息仅复制文本到word中再复制到pycharm中,否则很难调整格式。。或者使用视频中老师推荐的EditPlus来调整格式。
完整代码如下:
import urllib.request
import urllib.parse
import json
url = 'https://fanyi.baidu.com/v2transapi?from=sk&to=zh'
headers = {
'Accept': '*/*',
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
'Connection': 'keep-alive',
'Content-Length': '136',
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'Cookie': 'REALTIME_TRANS_SWITCH=1; FANYI_WORD_SWITCH=1; SOUND_SPD_SWITCH=1; HISTORY_SWITCH=1; SOUND_PREFER_SWITCH=1; PSTM=1593501691; BIDUPSID=A47F83355A98D20934011896B3A00A8E; __yjs_duid=1_e7408e6ce38b784195407c5e2a62042a1619433997218; BAIDUID=C1B32D9F9EDFEE1FD47D706658A7426E:FG=1; APPGUIDE_10_0_2=1; MCITY=-354%3A; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; BDUSS=y1YOUwwV1ppY0M1ZXozUzlBSXQtMWtETDJaWjY4MGwxckpMd0VyV1R1N0pNa2xpRVFBQUFBJCQAAAAAAAAAAAEAAADMioMVY2hhb2ppY2hhbmcAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMmlIWLJpSFib; BDUSS_BFESS=y1YOUwwV1ppY0M1ZXozUzlBSXQtMWtETDJaWjY4MGwxckpMd0VyV1R1N0pNa2xpRVFBQUFBJCQAAAAAAAAAAAEAAADMioMVY2hhb2ppY2hhbmcAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMmlIWLJpSFib; H_WISE_SIDS=110085_127969_174434_179345_184716_189755_190624_191068_191371_192383_194085_194512_195343_195467_195785_196426_197242_197711_197956_198270_198418_199022_199466_199571_200150_200993_201191_201545_201577_201701_202042_202058_202283_202545_202759_202822_202847_202894_202911_203075_203197_203267_203309_203361_203517_203525_203605_203885_204129_204132_204155_204200_204406_204437_204675_204823_204859_204918_204941_204947_204954_204973_205009_205087_205218_205234_205382_205413_205554_205690_205831_205856_205883_205958_206096_206284_206343_206515_206729_206814_206902_207026; RT="z=1&dm=baidu.com&si=zhsueosfpmb&ss=l0pc5xoz&sl=4&tt=2cd&bcn=https%3A%2F%2Ffclog.baidu.com%2Flog%2Fweirwood%3Ftype%3Dperf&ld=6xn&ul=coe&hd=cpg"; BDRCVFR[feWj1Vr5u3D]=I67x6TjHwwYf0; delPer=0; PSINO=2; BAIDUID_BFESS=F8C6A1122E4B088F20E05168D7D295FC:FG=1; ZD_ENTRY=baidu; Hm_lvt_64ecd82404c51e03dc91cb9e8c025574=1646894340,1647058167,1647138813,1647251899; H_PS_PSSID=35970_35106_31660_34813_36089_34584_35872_36077_35948_35801_35956_35984_35318_26350_35883_35724_35878_22159_36061; BA_HECTOR=ak042gah2h042l2l591h2u4g90q; Hm_lpvt_64ecd82404c51e03dc91cb9e8c025574=1647251978; ab_sr=1.0.1_MWNkMGNkOTExNjBhYzczNjk5ZmEzMTliODNlOTVhOTAwYzg1Mzk3OTA3M2YxNDRiZTU5YTFlZTUwMmYyNWY2MmM1ZDc3MDBhYmY1ZDJmZmVlNmNhOGIyMTBmZTM3OTkwY2Q1ZDc0NDcyOTQ1ZjYzZGNjYjY2ODIwY2E0YWVjZTUyNTgzZDZkMzE3ZDgyNzRiYmY0NjIzZmIxNmViMzQyNDI3MDAwOGE1OTBmNzZkZDAyZTVkYjljODYxMjEzNWEz',
'Host': 'fanyi.baidu.com',
'Origin': 'https://fanyi.baidu.com',
'Referer': 'https://fanyi.baidu.com/?aldtype=16047',
'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="98", "Google Chrome";v="98"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"Windows"',
'Sec-Fetch-Dest': 'empty',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Site': 'same-origin',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36',
'X-Requested-With': 'XMLHttpRequest'
}
data = {
'from': 'en',
'to': 'zh',
'query': 'spider',
'transtype': 'realtime',
'simple_means_flag': '3',
'sign': '63766.268839',
'token': '76d3f2542aa48c6b19fbcab7330a357c',
'domain': 'common'
}
data = urllib.parse.urlencode(data).encode('utf-8')
request = urllib.request.Request(url=url, data=data, headers=headers)
res = urllib.request.urlopen(request)
content = res.read().decode('utf-8')
content = json.loads(content)
print(content)
print(type(content))
打印结果:
需要注意的问题
- 请求头中的Accept-Encoding必须要注释掉
- 实际有用的信息只有Cookie和User-Agent,只留这两个结果一样
总结
- 找post请求的接口(F12)获取url和Form Data中的参数
- data参数必须要进行编码,还要调用encode()方法
- 请求对象的定制,传入data(data=data)
- 模拟浏览器向服务器发送请求(Cookie和User-Agent)
- 获取响应的数据
- 反序列化后打印结果
|