post请求和get请求都是客户端对浏览器发送的请求,但是post请求的特殊之处在于,它不会把参数直接放在url中,它的参数是隐藏起来的
以百度翻译为例,首先找到执行翻译的post请求的url地址:
此时在页面输入spider进行查询,下面的network会刷新很多信息,我们对其进行一一检查,首先过滤掉后缀为jpg和png的文件,它们是图片,肯定不能进行翻译。
那么找到几个差不多的,点开看一下:
在preview中可以看到查询结果是:蜘蛛
那么headers中的url地址就是我们需要的url:
可以看到post请求中并没有出现spider关键字,这是因为spider关键字被隐藏起来了。
下面这段代码用来获取post请求的网页源码:
# post请求 import urllib.request import urllib.parse
url = 'https://fanyi.baidu.com/v2transapi?from=en&to=zh'
headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.36' }
data = { 'query':'spider' }
# post请求的参数需要编码,因为post请求中不能出现字符串,必须是字节 data = urllib.parse.urlencode(data).encode('utf-8') print(data)
# post请求的参数不会拼接在url中,需要放在请求对象定制的参数中 request = urllib.request.Request(url = url, data = data, headers = headers)
print(request)
# 模拟浏览器向服务器发送请求 response = urllib.request.urlopen(request)
#获取响应的数据 content = response.read().decode('utf-8') print(content)
post请求方式的特点:
得到的结果是经过编码的string类型的json数据,需要将其进行处理:
# post请求 import urllib.request import urllib.parse
url = 'https://fanyi.baidu.com/v2transapi?from=en&to=zh'
headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.36' }
data = { 'query':'spider' }
# post请求的参数需要编码,因为post请求中不能出现字符串,必须是字节 data = urllib.parse.urlencode(data).encode('utf-8') print(data)
# post请求的参数不会拼接在url中,需要放在请求对象定制的参数中 request = urllib.request.Request(url = url, data = data, headers = headers)
print(request)
# 模拟浏览器向服务器发送请求 response = urllib.request.urlopen(request)
#获取响应的数据 content = response.read().decode('utf-8') print(content) print(type(content))
# 字符串 – json对象 import json
obj = json.loads(content) print(obj)
处理完之后发现报错,errno:998,这是因为百度翻译越来越严格的反爬措施,只需要做如下修改:
# post请求 import urllib.request import urllib.parse
url = 'https://fanyi.baidu.com/v2transapi?from=en&to=zh'
headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.36', 'Cookie': 'BIDUPSID=F352E19EDFD18E88A6D111AE7CB7B3A8; PSTM=1611757517; __yjs_duid=1_6cc240ffe6476ab065efda6b1be0a0641621078616582; BDUSS=JPVHdLbkFsVGRiRWdCfjAwVmgyTi1XOTFVRE1mQTJ5T3dpOEtJcmljYy1oaU5oSUFBQUFBJCQAAAAAAAAAAAEAAACTSWqlAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD75-2A–ftgSm; BDUSS_BFESS=JPVHdLbkFsVGRiRWdCfjAwVmgyTi1XOTFVRE1mQTJ5T3dpOEtJcmljYy1oaU5oSUFBQUFBJCQAAAAAAAAAAAEAAACTSWqlAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD75-2A–ftgSm; BDSFRCVID=5DuOJeC62uh-in5epD52bcgumdlrgK3TH6aoVtIvkPJstU3I3eVnEG0PfM8g0KubJmfpogKKKgOTHICF_2uxOjjg8UtVJeC6EG0Ptf8g0M5; H_BDCLCKID_SF=JRkH_IIhtCvbfP0k-tcH244HqxbXqMJQW2OZ0l8KtDQjeU3FK4od5q09XfRUq4rMKC-q3brmWIQHDU7b5PTF5U-tqJQPB53IbK54KKJx3pCWeIJo5DcB2hFvhUJiBh-HBan7-pvIXKohJh7FM4tW3J0ZyxomtfQxtNRJ0DnjtpChbC_6DTKKejb0eUcMetnKa55KWD8KJ6rjDCklDURcy4LdjG5f35J0-D7-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Dlo3e4cXJCTjKIK-KJOJfJ6ghb5JyTt7yajK2h5JW6cWbpcbbUK5ODON34TpQT8rb4FOK5OibCrZWRQMab3vOpvTXpO1yftzBN5thURB2DkO-4bCWJ5TMl5jDh3Mb6ksD-FtqtJHKbDH_D0XtMK; H_PS_PSSID=35704_35104_31660_35733_35488_34584_35490_34813_35685_35318_26350_35723_22158; BAIDUID_BFESS=550F6054EA21CD0D288BAF00A8CBFD02:FG=1; BDRCVFR[feWj1Vr5u3D]=I67x6TjHwwYf0; PSINO=7; Hm_lvt_64ecd82404c51e03dc91cb9e8c025574=1643160768,1643160798,1643161238,1643164216; Hm_lpvt_64ecd82404c51e03dc91cb9e8c025574=1643164216; ab_sr=1.0.1_OWM5YmJiODE3Y2EzODk0YWQ1MmZlM2E2YThjZTU5OTZjOTA0OGU3MmYyYWU5OGZmNWFkMDYxY2U1ZDUzMTAxYmM5ZTEwNWRlNzI3ZjgxNWIxN2Q1YTc4MDQxNDEyMTY4NTc4Nzc4OGVhZjNiNjlkNmVlNmI0Y2MxODExNzVlNTM=; BA_HECTOR=0h2584842k810gagq71gv1d8t0r' }
data = { 'query':'spider', 'from': 'en', 'to': 'zh', 'query': 'spider', 'simple_means_flag': '3', 'sign': '63766.268839', 'token': '0b4ac1b928ed4d7adbaa2c24de87b13b', 'domain': 'common' }
# post请求的参数需要编码,因为post请求中不能出现字符串,必须是字节 data = urllib.parse.urlencode(data).encode('utf-8') print(data)
# post请求的参数不会拼接在url中,需要放在请求对象定制的参数中 request = urllib.request.Request(url = url, data = data, headers = headers)
print(request)
# 模拟浏览器向服务器发送请求 response = urllib.request.urlopen(request)
#获取响应的数据 content = response.read().decode('utf-8') print(content) print(type(content))
# 字符串 – json对象 import json
obj = json.loads(content) print(obj)
即增加sign和cookies信息。
cookies的获取方式如下:
sign的获取方式如下:
可以看到最后的执行结果为:
神龙|纯净稳定代理IP免费测试>>>>>>>>天启|企业级代理IP免费测试>>>>>>>>IPIPGO|全球住宅代理IP免费测试