本文摘自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如何计算1到100的和
Python实现对文件中图片生成带标签的txt文件方法
Python如何生成词云的方法
Python怎么运行代码程序
Python中logging模块有什么用?logging模块的用法介绍
Python面向对象之继承和多态
Python是强类型语言吗
Python怎么逆序输出三位数
Python3.6想使用urllib2包怎么办
Python新手入门之环境安装
更多相关阅读请进入《Python》频道 >>
人民邮电出版社
python入门书籍,非常畅销,超高好评,python官方公认好书。
转载请注明出处:木庄网络博客 » python怎么安装requests库