1. requests的一些方法
1.1 requests.request(请求)
参数解释:
- method: get/post/head/put/delete
- url
- params: 请求的参数(定制http的参数)
- data: 上传的数据:字典,字节流,类文件句柄
- json: 上传的json数据
- headers: 自定义http头
- cookie: 发送额外的cookies(字典),默认自动处理发送上一次的cookie
- verify: 是否检验证书
源代码中的解释:
def request(method, url, **kwargs):
"""Constructs and sends a :class:`Request <Request>`.
:param method: method for the new :class:`Request` object.
:param url: URL for the new :class:`Request` object.
:param params: (optional) Dictionary or bytes to be sent in the query string for the :class:`Request`.
:param data: (optional) Dictionary, bytes, or file-like object to send in the body of the :class:`Request`.
:param json: (optional) json data to send in the body of the :class:`Request`.
:param headers: (optional) Dictionary of HTTP Headers to send with the :class:`Request`.
:param cookies: (optional) Dict or CookieJar object to send with the :class:`Request`.
:param files: (optional) Dictionary of ``'name': file-like-objects`` (or ``{'name': ('filename', fileobj)}``) for multipart encoding upload.
:param auth: (optional) Auth tuple to enable Basic/Digest/Custom HTTP Auth.
:param timeout: (optional) How long to wait for the server to send data
before giving up, as a float, or a :ref:`(connect timeout, read
timeout) <timeouts>` tuple.
:type timeout: float or tuple
:param allow_redirects: (optional) Boolean. Set to True if POST/PUT/DELETE redirect following is allowed.
:type allow_redirects: bool
:param proxies: (optional) Dictionary mapping protocol to the URL of the proxy.
:param verify: (optional) whether the SSL cert will be verified. A CA_BUNDLE path can also be provided. Defaults to ``True``.
:param stream: (optional) if ``False``, the response content will be immediately downloaded.
:param cert: (optional) if String, path to ssl client cert file (.pem). If Tuple, ('cert', 'key') pair.
:return: :class:`Response <Response>` object
:rtype: requests.Response
Usage::
>>> import requests
>>> req = requests.request('GET', 'http://httpbin.org/get')
<Response [200]>
"""
requests.get
参数:
- url
- 其他参数和request的参数一样
requests.post
参数:
- url
- data
- json
- 和request参数一样
其他方法请求方法
- requests.head
- requests.put
- requests.delete
1.2 requests.Response(应答)
参数解释:
- status_code: 状态码
- headers: 应答的http头
- json: 应答的json数据
- text: 应答的unicode编码的文本
- content: 应答的字节流
- cookies: 应答的cookie数据
源代码中的解释
class Response(object):
"""The :class:`Response <Response>` object, which contains a
server's response to an HTTP request.
"""
__attrs__ = [
'_content', 'status_code', 'headers', 'url', 'history',
'encoding', 'reason', 'cookies', 'elapsed', 'request'
]
def __init__(self):
super(Response, self).__init__()
self._content = False
self._content_consumed = False
#: Integer Code of responded HTTP Status, e.g. 404 or 200.
self.status_code = None
#: Case-insensitive Dictionary of Response Headers.
#: For example, ``headers['content-encoding']`` will return the
#: value of a ``'Content-Encoding'`` response header.
self.headers = CaseInsensitiveDict()
#: File-like object representation of response (for advanced usage).
#: Use of ``raw`` requires that ``stream=True`` be set on the request.
# This requirement does not apply for use internally to Requests.
self.raw = None
#: Final URL location of Response.
self.url = None
#: Encoding to decode with when accessing r.text.
self.encoding = None
#: A list of :class:`Response <Response>` objects from
#: the history of the Request. Any redirect responses will end
#: up here. The list is sorted from the oldest to the most recent request.
self.history = []
#: Textual reason of responded HTTP Status, e.g. "Not Found" or "OK".
self.reason = None
#: A CookieJar of Cookies the server sent back.
self.cookies = cookiejar_from_dict({})
#: The amount of time elapsed between sending the request
#: and the arrival of the response (as a timedelta).
#: This property specifically measures the time taken between sending
#: the first byte of the request and finishing parsing the headers. It
#: is therefore unaffected by consuming the response content or the
#: value of the ``stream`` keyword argument.
self.elapsed = datetime.timedelta(0)
#: The :class:`PreparedRequest <PreparedRequest>` object to which this
#: is a response.
self.request = None
1.3 测试代码
# use_requests.py
import requests
def get_json():
r = requests.get('https://api.github.com/events')
print(r.status_code)
print(r.headers)
print(r.content)
print(r.text)
print(r.json())
def get_querystring(): # 传递两个字符串参数
url = 'http://httpbin.org/get'
params = {'qs1': 'value1', 'qs2': 'value2'} # 传递参数
r = requests.get(url, params=params)
print(r.status_code)
print(r.content.decode('utf-8'))
def get_custom_headers(): # 传递http头
url = 'http://httpbin.org/get'
headers = {'x-header1': 'value1', 'x-header2': 'value2'}
r = requests.get(url, headers=headers)
print(r.status_code)
print(r.content.decode('utf-8'))
def get_cookie(): # 测试cookie
headers = {'User-Agent': 'Chrome'}
url = 'http://www.douban.com'
r = requests.get(url, headers=headers)
print(r.status_code)
print(r.cookies)
print(r.cookies['bid'])
if __name__ == '__main__':
#get_json()
#get_querystring()
#get_custom_headers()
get_cookie()
2. requests的高级用法
2.1 Session
用一个回话内参数保持一致,且会重用TCP连接以提高性能。
2.2 SSL证书认证
开启、关闭、自定义CA证书