码迷,mamicode.com
首页 > 编程语言 > 详细

python-requests包

时间:2020-04-04 16:15:06      阅读:68      评论:0      收藏:0      [点我收藏+]

标签:recent   bsp   list   ons   encoding   strong   传递参数   常见   nbsp   

Requests库使用

1.作用

提供了HTTP协议的所有请求方式(如get,post,put,delete等)

2.安装

pip install requests

3.常见用法

  • 发送GET请求(不传参数)

>>> import requests
>>> resp = requests.get(‘https://www.baidu.com‘)
  • 发送GET请求(传递参数)

>>> import requests
>>> params = {‘key1‘: ‘value1‘, ‘key2‘: ‘value2‘}
>>> resp = requests.get("http://httpbin.org/get", params=params)
>>> print(resp.url)
http://httpbin.org/get?key2=value2&key1=value1
  • 发送POST请求

>>> import requests
>>> resp = requests.post(‘http://httpbin.org/post‘, data = {‘key‘:‘value‘})
  • 发送其他类型的请求

>>> resp = requests.put(‘http://httpbin.org/put‘, data = {‘key‘:‘value‘})
>>> resp = requests.delete(‘http://httpbin.org/delete‘)
>>> resp = requests.head(‘http://httpbin.org/get‘)
>>> resp = requests.options(‘http://httpbin.org/get‘)
  • 发送请求时,可以设置:Headers,Cookies,代理,禁止证书验证,超时

    设置Headers

>>> url = ‘https://api.github.com/some/endpoint‘
>>> headers = {‘user-agent‘: ‘my-app/0.0.1‘}
>>> resp = requests.get(url, headers=headers)

设置Cookies

>>> url = ‘http://httpbin.org/cookies‘
>>> cookies = {‘cookies_are‘: ‘working‘}
?
>>> resp = requests.get(url, cookies=cookies)
>>> resp.text
‘{"cookies": {"cookies_are": "working"}}‘

设置代理

import requests
?
proxies = {
 ‘http‘: ‘http://10.10.1.10:3128‘,
 ‘https‘: ‘http://10.10.1.10:1080‘,
}
?
requests.get(‘http://example.org‘, proxies=proxies)

设置禁止证书验证

>>> import requests
>>> resp = requests.get(‘http://httpbin.org/post‘, verify=False)

设置超时

>>> requests.get(‘http://github.com‘, timeout=0.001)
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
requests.exceptions.Timeout: HTTPConnectionPool(host=‘github.com‘, port=80): Request timed out. (timeout=0.001)
  • 获取响应内容

    Requests通过text属性,就可以获得字符串格式的响应内容。

>>> import requests
?
>>> resp = requests.get(‘https://api.github.com/events‘)
>>> resp.text
u‘[{"repository":{"open_issues":0,"url":"https://github.com/...

Requests通过content属性,就可以获得二进制数据格式的响应内容。

>>> resp.content
b‘[{"repository":{"open_issues":0,"url":"https://github.com/...

Requests通过json方法,就可以获得JSON格式的响应内容。

>>> import requests
?
>>> resp = requests.get(‘https://api.github.com/events‘)
>>> resp.json()
[{u‘repository‘: {u‘open_issues‘: 0, u‘url‘: ‘https://github.com/...
  • 获取响应参数:响应状态码、响应报文头、url、服务器返回的cookies、响应的Session、

    获取响应状态码,通过status_code属性获取

>>> resp = requests.get(‘http://httpbin.org/get‘)
>>> resp.status_code
200

获取响应报文头,通过headers属性获取

>>> r.headers
{
   ‘content-encoding‘: ‘gzip‘,
   ‘transfer-encoding‘: ‘chunked‘,
   ‘connection‘: ‘close‘,
   ‘server‘: ‘nginx/1.0.4‘,
   ‘x-runtime‘: ‘148ms‘,
   ‘etag‘: ‘"e1ca502697e5c9317743dc078f67693f"‘,
   ‘content-type‘: ‘application/json‘
}

获取服务器返回的cookies,通过cookies属性获取,得到是一个字典

>>> url = ‘http://example.com/some/cookie/setting/url‘
>>> resp = requests.get(url)
>>> resp.cookies[‘example_cookie_name‘]
‘example_cookie_value‘

获取响应的url,通过url属性获取

>>> import requests
>>> params = {‘key1‘: ‘value1‘, ‘key2‘: ‘value2‘}
>>> resp = requests.get("http://httpbin.org/get", params=params)
>>> print(resp.url)
http://httpbin.org/get?key2=value2&key1=value1

 

  • 设置session(上边有设置cookie方法了)

import requests
?
session = requests.Session() # 建立一个session
?
session.get(‘http://httpbin.org/cookies/set/sessioncookie/123456789‘)   # 设置session
resp = session.get(‘http://httpbin.org/cookies‘)
?
print(resp.text)
# ‘{"cookies": {"sessioncookie": "123456789"}}‘

(如果你想要让一个headersSession的整个生命周期内都有效的话,需要用以下的方式来进行设置:)

# 设置整个headers
session.headers = {
   ‘user-agent‘: ‘my-app/0.0.1‘
}
# 增加一条headers
session.headers.update({‘x-test‘: ‘true‘})

 

python-requests包

标签:recent   bsp   list   ons   encoding   strong   传递参数   常见   nbsp   

原文地址:https://www.cnblogs.com/su-sir/p/12632214.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!