Python requests和httpx实例详解
更新时间:2023年12月26日 10:43:17 作者:麦子磨成面筋
这篇文章主要介绍了Python requests和httpx的相关知识,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧
Python requests和httpx
1. 获取cookies
1.1 requests获取cookies
1.1.1 直接获取cookies
response = requests.get(url=url, headers=headers) response.cookies.items()
1.1.2 session 获取cookies
session = requests.session()
1.2 httpx获取cookie
response.cookies.items()
1.3 获取Set-Cookie
response = requests.get(url=url, headers=headers)
set_cookies = response.headers.get('Set-Cookie')
# 注意,如果是重定向的话,会获取不到set-cookie,需要allow_redirects=False来禁止重定向
response = requests.get(url=url, headers=headers, allow_redirects=False)1.4 cookie 失效
3.1 将cookie放在cookies参数里
1.5 获取cookie的问题
1.5.1 发生了302重定向
看请求是否发生了302重定向
使用requests.Session()方法,会使该连接持久化
1.5.2 发生了跨域请求
2、添加代理(requests和httpx的代理样式不一样)
2.1 requests添加代理
proxies={
'http': 'http://ip:port',
'https': 'http://ip:port',
}2.1.1get或者post
import requests url="" response=requests.get(url=url, proxies=proxies)
2.1.2 session
import requests url="" session = requests.session() session.proxies.update(proxy)
2.2 httpx添加代理
import httpx
proxies = {
'http://': 'http://ip:port',
'https://': 'http://ip:port',
}
url = ""
response = httpx.get(url=url, proxies=proxies)到此这篇关于Python requests和httpx的文章就介绍到这了,更多相关Python requests和httpx内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
ChatGPT 帮我自动编写 Python 爬虫脚本的详细过程
ChatGPT是一种基于大语言模型的生成式AI,换句话说它可以自动生成类似人类语言的文本,把梳理好的有逻辑的答案呈现在你面前,这完全不同于传统搜索工具,这篇文章主要介绍了ChatGPT 帮我自动编写 Python 爬虫脚本,需要的朋友可以参考下2023-02-02
Python爬虫scrapy框架Cookie池(微博Cookie池)的使用
这篇文章主要介绍了Python爬虫scrapy框架Cookie池(微博Cookie池)的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2021-01-01
python时间日期函数与利用pandas进行时间序列处理详解
python标准库包含于日期(date)和时间(time)数据的数据类型,datetime、time以及calendar模块会被经常用到,而pandas则可以对时间进行序列化排序2018-03-03


最新评论