Python requests和httpx實(shí)例詳解
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') # 注意,如果是重定向的話,會(huì)獲取不到set-cookie,需要allow_redirects=False來禁止重定向 response = requests.get(url=url, headers=headers, allow_redirects=False)
1.4 cookie 失效
3.1 將cookie放在cookies參數(shù)里
1.5 獲取cookie的問題
1.5.1 發(fā)生了302重定向
看請求是否發(fā)生了302重定向
使用requests.Session()方法,會(huì)使該連接持久化
1.5.2 發(fā)生了跨域請求
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)
到此這篇關(guān)于Python requests和httpx的文章就介紹到這了,更多相關(guān)Python requests和httpx內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用Python進(jìn)行大規(guī)模數(shù)據(jù)處理和分析
大規(guī)模數(shù)據(jù)處理和分析旨在從海量數(shù)據(jù)中提取有用的信息和見解,以支持決策制定和業(yè)務(wù)發(fā)展,Python憑借其豐富的生態(tài)系統(tǒng)和強(qiáng)大的庫,為處理和分析數(shù)據(jù)提供了豐富的工具和資源,在本文中,我們將深入探討如何利用Python進(jìn)行大規(guī)模數(shù)據(jù)處理和分析,需要的朋友可以參考下2024-05-05在Python3中使用asyncio庫進(jìn)行快速數(shù)據(jù)抓取的教程
這篇文章主要介紹了在Python3中使用asyncio進(jìn)行快速數(shù)據(jù)抓取,asyncio是一個(gè)異步IO庫,運(yùn)行效率較高,需要的朋友可以參考下2015-04-04Django零基礎(chǔ)入門之調(diào)用漂亮的HTML前端頁面
這篇文章主要介紹了Django零基礎(chǔ)入門之調(diào)用漂亮的HTML前端頁面的方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-09-09Python實(shí)現(xiàn)pdf文檔轉(zhuǎn)txt的方法示例
這篇文章主要介紹了Python實(shí)現(xiàn)pdf文檔轉(zhuǎn)txt的方法,結(jié)合實(shí)例形式分析了Python基于第三方庫pdfminier實(shí)現(xiàn)針對pdf格式文檔的讀取、轉(zhuǎn)換等相關(guān)操作技巧,需要的朋友可以參考下2018-01-01