Python3 爬虫 httpx 请求库的使用;

urllib 和requests 请求库只能支持http1.1 的请求, 对于http2 是无能为力的, httpx 请求库就是专门处理 http2 请求的。

安装:

pip install httpx  

# 如果使用http2.0 则需要指定以下
pip install httpx[http2]

基本使用:

httpx 和requests请求库有着很多相似之处, 所以用法也大同小异

import httpx

response = httpx.get('https://httpbin.org/get')
print(response.status_code)
print(response.headers)
print(response.text)

添加请求头:

import httpx

headers = {
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36'
}
response = httpx.get('https://httpbin.org/get', headers=headers)

print(response.text)

httpx 默认使用的还是http1.1 协议, 需要手动开启 http2.0 请求才可以使用

import httpx
client = httpx.Client(http2=True)

response = client.get(
    'https://spa16.scrape.center/')
print(response.text)

Client对象:

httpx 请求库中, 有一个 Client 对象, 可以与requests 库中的 Session 对象类比学习

import httpx

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

使用 with as 方式等价于以下 try 的使用方式:

client = httpx.Client()
try:
    response = client.get('https://httpbin.org/get')
finally:
    client.close()

以上 两种 Client 对象的使用是等价的, 唯一不同的只是需要手动的去调用一下client对象的关闭方法。

另外, 在使用Client对象的时候, 可以指定一些参数, 例如 headers,


url = 'http://httpbin.org/headers'
headers = {'User-Agent': 'my-app/0.0.1'}

with httpx.Client(headers=headers) as client:
    r = client.get(url)
    print(r.json()['headers']['User-Agent'])

关于Client 对象的更高级用法参考官方:

https://www.python-httpx.org/advanced/

使用Client 对象开启 http2.0:

import httpx
client = httpx.Client(http2=True)

response = client.get('https://httpbin.org/get')
print(response.text)
print(response.http_version)

AsyncClient:

httpx 请求库还支持异步请求:

import httpx
import asyncio


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

if __name__ == '__main__':
    asyncio.get_event_loop().run_until_complete(fetch('https://httpbin.org/get'))

  • 9
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

_文书先生

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

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

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

打赏作者

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

抵扣说明:

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

余额充值