python get请求 header_python3 get/post/使用代理/自定义header/自定义Cookie

说明:urllib发送http请求并不是很人性化,更推荐使用在urllib基础上封装的、python2和python3都兼容的requests模块,移步查看。

一、get请求

get请求就是在构造Request对象时,只传入url参数

更多的教程发送get请求的写法直接是不构造Request对象而直接urllib.request.urlopen(url),但为了与后边post和header的统一推荐还是构造Request。

importurllib.request

url_request="http://10.10.6.92/Pages/login.htm"request_obj=urllib.request.Request(url=url_request)

response_obj=urllib.request.urlopen(request_obj)

html_code=response_obj.read().decode('utf-8')print(html_code)

二、post请求

post请求与get的区别,是在构造Request对象时除了传入url参数还要传入data参数

不用怀疑,真的加上data,发送数据时就自动由get方法改为post方法

当然其实也可以在构造Request时多传入method参数(比如method='POST'),强制使用某种方法;

不过注意这里的method参数只是生硬地用该参数的值去替换请求动词(比如你设为‘XX’,请求动词就是‘XX’),其他东西不会变的(比如有post_data强传‘GET’,数据还是用POST的形式提交)。

importurllib.request

url_request="http://10.10.6.92/Pages/login.htm"post_data='<?xml version="1.0" encoding="utf-8" ?>MTIzNDU2'post_data=post_data.encode('utf-8')

request_obj=urllib.request.Request(url=url_request,data=post_data)

response_obj=urllib.request.urlopen(request_obj)

html_code=response_obj.read().decode('utf-8')print(html_code)

三、使用代理

使用代理的关键是下边中间的四行代码

同样强调使用代理和是get方法,还是post方法,还是使用代理都没有关联

import urllib.request

url_request="http://10.10.6.92/Pages/login.htm"

request_obj=urllib.request.Request(url=url_request)

request_obj.set_proxy('127.0.0.1:8080','http')

response_obj=urllib.request.urlopen(request_obj)

html_code=response_obj.read().decode('utf-8')

print(html_code)

python默认发送头部如下,这四个值和自定义头部类似于父类和子类的关系:

四、自定义header

自定义header,就是在构造Request对象时多传入headers参数

header与是get方法还是post方法是没有关联的;也就是说post方法想自定义头部,那么在post的基础方多传入headers参数即可

importurllib.request

url_request="http://10.10.6.92/Pages/login.htm"header_selfdefine={'Host':'10.10.6.92','User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:59.0) Gecko/20100101 Firefox/59.0','Accept': '*/*','Accept-Language': 'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2','Accept-Encoding': 'gzip, deflate','Referer': 'http://10.10.6.92/Pages/login.htm'}

request_obj=urllib.request.Request(url=url_request,headers=header_selfdefine)

response_obj=urllib.request.urlopen(request_obj)

html_code=response_obj.read().decode('utf-8')print(html_code)

五、自定义Cookie

如果使用add_header添加的请求头在header中已经存在,那么header中同一请求头的值将被覆盖;或者叫以add_header的为准。

importurllib.request

url_request= 'http://10.10.6.92/Pages/login.htm'request_obj=urllib.request.Request(url=url_request)

request_obj.add_header('Cookie','username="root", password="toor"')

response_obj=urllib.request.urlopen(request_obj)

html_code= response_obj.read().decode('utf-8')print(html_code)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python模拟登录通常使用requests库来实现,以下是三种常见的方式: 1. 基于session的登录方式 ```python import requests # 创建session对象 s = requests.Session() # 登录接口 login_url = "http://example.com/login" # 请求参数 data = { "username": "your_username", "password": "your_password" } # 登录 s.post(login_url, data=data) # 访问需要登录才能访问的页面 response = s.get("http://example.com/profile") ``` 2. 基于cookie的登录方式 ```python import requests # 登录接口 login_url = "http://example.com/login" # 请求参数 data = { "username": "your_username", "password": "your_password" } # 登录 response = requests.post(login_url, data=data) # 获取cookie cookie_dict = response.cookies.get_dict() # 使用cookie访问需要登录才能访问的页面 response = requests.get("http://example.com/profile", cookies=cookie_dict) ``` 3. 基于token的登录方式 ```python import requests # 登录接口 login_url = "http://example.com/login" # 请求参数 data = { "username": "your_username", "password": "your_password" } # 登录 response = requests.post(login_url, data=data) # 获取token token = response.json().get("token") # 设置header中的Authorization字段 headers = { "Authorization": f"Bearer {token}" } # 使用token访问需要登录才能访问的页面 response = requests.get("http://example.com/profile", headers=headers) ``` 以上三种方式都可以实现模拟登录,具体使用哪种方式取决于登录接口的实现方式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值