使用python3模拟http头部信息

有的网站为了防止恶意爬取数据,对UA头进行了校验,判断是否是来自于浏览器的请求,我们使用python3请求的UA头是 “User-Agent”: “Python-urllib/3.5”,只要将UA头指定为浏览器UA头或者任意内容。

root@VM-0-7-ubuntu:~/python/zeropython3/15# cat post_4.py 
from urllib import parse
from urllib import request

headers={
"User-Agent":"I am UA!"
}

data = bytes(parse.urlencode({'hello':'world'}),encoding = 'utf8')

req = request.Request('http://httpbin.org/post',data=data,headers=headers,method = 'POST')
response = request.urlopen(req)
print (response.read().decode('utf-8'))
------------------------------------------------------------------------------------------
root@VM-0-7-ubuntu:~/python/zeropython3/15# python3 post_4.py 
{
  "args": {}, 
  "data": "", 
  "files": {}, 
  "form": {
    "hello": "world"
  }, 
  "headers": {
    "Accept-Encoding": "identity", 
    "Content-Length": "11", 
    "Content-Type": "application/x-www-form-urlencoded", 
    "Host": "httpbin.org", 
    "User-Agent": "I am UA!"  #成功替换
  }, 
  "json": null, 
  "origin": "xxxxx, xxxxxxx", 
  "url": "https://httpbin.org/post"
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LQxdp

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值