亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

python中httpx庫(kù)的詳細(xì)使用方法及案例詳解

 更新時(shí)間:2025年02月27日 09:38:54   作者:數(shù)據(jù)知道  
httpx 是一個(gè)現(xiàn)代化的 Python HTTP 客戶端庫(kù),支持同步和異步請(qǐng)求,功能強(qiáng)大且易于使用,它比 requests 更高效,支持 HTTP/2 和異步操作,以下是 httpx 的詳細(xì)使用方法,感興趣的小伙伴跟著小編一起來(lái)看看吧

1. 安裝 httpx

首先,確保已經(jīng)安裝了 httpx??梢酝ㄟ^(guò)以下命令安裝:pip install httpx

如果需要支持 HTTP/2,可以安裝額外依賴:pip install httpx[http2]

2. 同步請(qǐng)求

發(fā)送 GET 請(qǐng)求

import httpx

# 發(fā)送 GET 請(qǐng)求
response = httpx.get('https://httpbin.org/get')
print(response.status_code)  # 狀態(tài)碼
print(response.text)         # 響應(yīng)內(nèi)容

發(fā)送 POST 請(qǐng)求

# 發(fā)送 POST 請(qǐng)求
data = {'key': 'value'}
response = httpx.post('https://httpbin.org/post', json=data)
print(response.json())  # 解析 JSON 響應(yīng)

設(shè)置請(qǐng)求頭

headers = {'User-Agent': 'my-app/1.0.0'}
response = httpx.get('https://httpbin.org/headers', headers=headers)
print(response.json())

設(shè)置查詢參數(shù)

params = {'key1': 'value1', 'key2': 'value2'}
response = httpx.get('https://httpbin.org/get', params=params)
print(response.json())

處理超時(shí)

try:
    response = httpx.get('https://httpbin.org/delay/5', timeout=2.0)
except httpx.TimeoutException:
    print("請(qǐng)求超時(shí)")

3. 異步請(qǐng)求

httpx 支持異步操作,適合高性能場(chǎng)景。

發(fā)送異步 GET 請(qǐng)求

import httpx
import asyncio

async def fetch(url):
    async with httpx.AsyncClient() as client:
        response = await client.get(url)
        print(response.text)

asyncio.run(fetch('https://httpbin.org/get'))

發(fā)送異步 POST 請(qǐng)求

async def post_data(url, data):
    async with httpx.AsyncClient() as client:
        response = await client.post(url, json=data)
        print(response.json())

asyncio.run(post_data('https://httpbin.org/post', {'key': 'value'}))

并發(fā)請(qǐng)求

async def fetch_multiple(urls):
    async with httpx.AsyncClient() as client:
        tasks = [client.get(url) for url in urls]
        responses = await asyncio.gather(*tasks)
        for response in responses:
            print(response.text)

urls = ['https://httpbin.org/get', 'https://httpbin.org/ip']
asyncio.run(fetch_multiple(urls))

4. 高級(jí)功能

使用 HTTP/2

# 啟用 HTTP/2
client = httpx.Client(http2=True)
response = client.get('https://httpbin.org/get')
print(response.http_version)  # 輸出協(xié)議版本

文件上傳

files = {'file': open('example.txt', 'rb')}
response = httpx.post('https://httpbin.org/post', files=files)
print(response.json())

流式請(qǐng)求

# 流式上傳
def generate_data():
    yield b"part1"
    yield b"part2"

response = httpx.post('https://httpbin.org/post', data=generate_data())
print(response.json())

流式響應(yīng)

# 流式下載
with httpx.stream('GET', 'https://httpbin.org/stream/10') as response:
    for chunk in response.iter_bytes():
        print(chunk)

5. 錯(cuò)誤處理

httpx 提供了多種異常類,方便處理錯(cuò)誤。

處理網(wǎng)絡(luò)錯(cuò)誤

try:
    response = httpx.get('https://nonexistent-domain.com')
except httpx.NetworkError:
    print("網(wǎng)絡(luò)錯(cuò)誤")

處理 HTTP 錯(cuò)誤狀態(tài)碼

response = httpx.get('https://httpbin.org/status/404')
if response.status_code == 404:
    print("頁(yè)面未找到")

6. 配置客戶端

可以通過(guò) httpx.Client 或 httpx.AsyncClient 配置全局設(shè)置。

設(shè)置超時(shí)

client = httpx.Client(timeout=10.0)
response = client.get('https://httpbin.org/get')
print(response.text)

設(shè)置代理

proxies = {
    "http://": "http://proxy.example.com:8080",
    "https://": "http://proxy.example.com:8080",
}
client = httpx.Client(proxies=proxies)
response = client.get('https://httpbin.org/get')
print(response.text)

設(shè)置基礎(chǔ) URL

client = httpx.Client(base_url='https://httpbin.org')
response = client.get('/get')
print(response.text)

7. 結(jié)合 Beautiful Soup 使用

httpx 可以與 Beautiful Soup 結(jié)合使用,抓取并解析網(wǎng)頁(yè)。

import httpx
from bs4 import BeautifulSoup

# 抓取網(wǎng)頁(yè)
response = httpx.get('https://example.com')
html = response.text

# 解析網(wǎng)頁(yè)
soup = BeautifulSoup(html, 'lxml')
title = soup.find('title').text
print("網(wǎng)頁(yè)標(biāo)題:", title)

8. 示例:抓取并解析網(wǎng)頁(yè)

以下是一個(gè)完整的示例,展示如何使用 httpx 抓取并解析網(wǎng)頁(yè)數(shù)據(jù):

import httpx
from bs4 import BeautifulSoup

# 抓取網(wǎng)頁(yè)
url = 'https://example.com'
response = httpx.get(url)
html = response.text

# 解析網(wǎng)頁(yè)
soup = BeautifulSoup(html, 'lxml')

# 提取標(biāo)題
title = soup.find('title').text
print("網(wǎng)頁(yè)標(biāo)題:", title)

# 提取所有鏈接
links = soup.find_all('a', href=True)
for link in links:
    href = link['href']
    text = link.text
    print(f"鏈接文本: {text}, 鏈接地址: {href}")

9. 注意事項(xiàng)

性能:httpx 的異步模式適合高并發(fā)場(chǎng)景。

兼容性:httpx 的 API 與 requests 高度兼容,遷移成本低。

HTTP/2:如果需要使用 HTTP/2,確保安裝了 httpx[http2]。

通過(guò)以上方法,可以使用 httpx 高效地發(fā)送 HTTP 請(qǐng)求,并結(jié)合其他工具(如 Beautiful Soup)實(shí)現(xiàn)數(shù)據(jù)抓取和解析。

到此這篇關(guān)于python中httpx庫(kù)的詳細(xì)使用方法及案例詳解的文章就介紹到這了,更多相關(guān)python httpx庫(kù)使用及案例內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • python實(shí)現(xiàn)抖音視頻批量下載

    python實(shí)現(xiàn)抖音視頻批量下載

    這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)抖音視頻批量下載,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-06-06
  • python sys模塊使用方法介紹

    python sys模塊使用方法介紹

    sys模塊是最常用的和python解釋器交互的模塊,sys模塊可供訪問(wèn)由解釋器(interpreter)使用或維護(hù)的變量和與解釋器進(jìn)行交互的函數(shù),需要的朋友可以參考下
    2022-08-08
  • python獲取字符串中的email

    python獲取字符串中的email

    這篇文章主要介紹了python獲取字符串中的email,通過(guò)調(diào)用re庫(kù),通過(guò)使用compile、findall獲取字符串中的email,下文詳細(xì)實(shí)現(xiàn)過(guò)程需要的小伙伴可以參考一下
    2022-03-03
  • flask/django 動(dòng)態(tài)查詢表結(jié)構(gòu)相同表名不同數(shù)據(jù)的Model實(shí)現(xiàn)方法

    flask/django 動(dòng)態(tài)查詢表結(jié)構(gòu)相同表名不同數(shù)據(jù)的Model實(shí)現(xiàn)方法

    今天小編就為大家分享一篇flask/django 動(dòng)態(tài)查詢表結(jié)構(gòu)相同表名不同數(shù)據(jù)的Model實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-08-08
  • Python3 操作 MySQL 插入一條數(shù)據(jù)并返回主鍵 id的實(shí)例

    Python3 操作 MySQL 插入一條數(shù)據(jù)并返回主鍵 id的實(shí)例

    這篇文章主要介紹了Python3 操作 MySQL 插入一條數(shù)據(jù)并返回主鍵 id的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-03-03
  • Python運(yùn)行不顯示DOS窗口的解決方法

    Python運(yùn)行不顯示DOS窗口的解決方法

    今天小編就為大家分享一篇Python運(yùn)行不顯示DOS窗口的解決方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-10-10
  • 詳解Python中的靜態(tài)方法與類成員方法

    詳解Python中的靜態(tài)方法與類成員方法

    這篇文章主要介紹了關(guān)于Python中靜態(tài)方法與類成員的相關(guān)資料,文中通過(guò)示例代碼給大家詳細(xì)總結(jié)了兩者在語(yǔ)法和使用上的區(qū)別,有需要的朋友可以參考借鑒,下面來(lái)一起看看吧。
    2017-02-02
  • python圖形界面tkinter的使用技巧

    python圖形界面tkinter的使用技巧

    這篇文章主要介紹了python圖形界面tkinter的使用技巧,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-09-09
  • 用?Python?繪制全國(guó)鴻星爾克門店分布圖

    用?Python?繪制全國(guó)鴻星爾克門店分布圖

    這篇文章主要介紹了用?Python?繪制全國(guó)鴻星爾克門店分布圖,今天就以某度地圖?用Python爬蟲(chóng)看一下全國(guó)到底有多少家鴻星爾克門店,,需要的朋友可以參考一下
    2022-01-01
  • python記錄程序運(yùn)行時(shí)間的三種方法

    python記錄程序運(yùn)行時(shí)間的三種方法

    這篇文章主要介紹了python記錄程序運(yùn)行時(shí)間的三種方法的相關(guān)資料,需要的朋友可以參考下
    2017-07-07

最新評(píng)論