python中ht_在Python中使用httpx模块

导入httpx

In [25]: import httpx

获取一个网页

In [26]: r = httpx.get("https://httpbin.org/get")

In [27]: r

Out[27]:

同样,发送HTTP POST请求:

In [28]: r = httpx.post("https://httpbin.org/post", data={"key": "value"})

In [29]: r

Out[29]:

PUT,DELETE,HEAD和OPTIONS请求都遵循相同的方式:

In [35]: r = httpx.put("https://httpbin.org/put", data={"key": "value"})

In [36]: r = httpx.delete("https://httpbin.org/delete")

In [37]: r = httpx.head("https://httpbin.org/get")

In [38]: r = httpx.options("https://httpbin.org/get")

在URL中传递参数

在请求URL中传递查询参数,请使用params关键字 In [41]: r = httpx.get("https://httpbin.org/get", params=params)

In [42]: r

Out[42]:

检查发出请求结果的URL

2. 将项目列表作为值传递

In [44]: params = {"key1": "value2", "key2": ["value2", "value3"]}

In [45]: r = httpx.get("https://httpbin.org/get", params=params)

### 响应内容

HTTPX将自动处理响应内容解码为`Unicode`文本

In [52]: r = httpx.get("https://www.example.org/")

In [53]: r.text

Out[53]: '\n\n

\n Example Domain\n\n \n \n \n \n\n\n\n
\n

Example Domain

\n

This domain is for use in illustrative examples in documents. You may use this\n domain in literature without prior coordination or asking for permission.

\n

More information...

\n
\n\n\n'

解码

In [54]: r.encoding

Out[54]: 'UTF-8'

设置需要使用的编码

In [55]: r.encoding = "ISO-8859-1"

In [56]: print(r.headers, r.http_version, r.url, r.status_code)

Headers({'content-encoding': 'gzip', 'age': '532553', 'cache-control': 'max-age=604800', 'content-type': 'text/html; charset=UTF-8', 'date': 'Sat, 06 Jun 2020 04

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值