本文摘自php中文网,作者silencement,侵删。

requests是python实现的简单易用的HTTP库,使用起来比urllib简洁很多
因为是第三方库,所以使用前需要cmd安装
安装完成后import一下,正常则说明可以开始使用了。
基本用法:
requests.get()用于请求目标网站,类型是一个HTTPresponse类型
1 2 3 4 5 6 7 8 | import requests
response = requests.get('http://www.baidu.com')
print(response.status_code) # 打印状态码
print(response.url) # 打印请求url
print(response.headers) # 打印头信息
print(response.cookies) # 打印cookie信息print(response.text) #以文本形式打印网页源码
print(response.content) #以字节流形式打印
|
运行结果:
状态码:200
各种请求方式:
1 2 3 4 5 6 7 8 | import requests
requests.get('http://httpbin.org/get')
requests.post('http://httpbin.org/post')
requests.put('http://httpbin.org/put')
requests.delete('http://httpbin.org/delete')
requests.head('http://httpbin.org/get')
requests.options('http://httpbin.org/get')
|
基本的get请求
1 2 3 | import requests
response = requests.get('http://httpbin.org/get')print(response.text)
|
带参数的GET请求:
第一种直接将参数放在url内
1 2 3 | import requests
response = requests.get(http://httpbin.org/get?name=gemey&age=22)print(response.text)
|
解析json
1 2 3 4 5 6 | import requests
response = requests.get('http://httpbin.org/get')
print(response.text)
print(response.json()) #response.json()方法同json.loads(response.text)
print(type(response.json()))
|
以上就是python怎么安装requests库的详细内容,更多文章请关注木庄网络博客!!
相关阅读 >>
在Python里如何读取stdin
2021年Python面试题汇总(最新)
推荐几本关于的Python好书
Python设置环境变量有什么用?
golang中参数传递方式方法
Python eval的常见错误封装及利用原理的介绍
Python编程快速上手适合初学者吗
爬虫为什么不用java要用 Python
Python全栈工程师学些什么
Python属于开源语言吗
更多相关阅读请进入《Python》频道 >>
人民邮电出版社
python入门书籍,非常畅销,超高好评,python官方公认好书。
转载请注明出处:木庄网络博客 » python怎么安装requests库