requests庫
requests庫介紹:
Requests是一個優雅而簡單的Python HTTP庫,專爲人類而構建。(來自官方的介紹)html
requests
是一個很實用的Python HTTP客戶端庫,編寫爬蟲和測試服務器響應數據時常常會用到,對測試來講作接口測試會很方便。node
request庫使用
requests庫安裝:
requests是第三方的庫,須要先安裝 :pip3 install requests(python3版本)python
requests源碼


1 def request(method, url, **kwargs): 2 """Constructs and sends a :class:`Request <Request>`. 3 4 :param method: method for the new :class:`Request` object. 5 :param url: URL for the new :class:`Request` object. 6 :param params: (optional) Dictionary, list of tuples or bytes to send 7 in the query string for the :class:`Request`. 8 :param data: (optional) Dictionary, list of tuples, bytes, or file-like 9 object to send in the body of the :class:`Request`. 10 :param json: (optional) A JSON serializable Python object to send in the body of the :class:`Request`. 11 :param headers: (optional) Dictionary of HTTP Headers to send with the :class:`Request`. 12 :param cookies: (optional) Dict or CookieJar object to send with the :class:`Request`. 13 :param files: (optional) Dictionary of ``'name': file-like-objects`` (or ``{'name': file-tuple}``) for multipart encoding upload. 14 ``file-tuple`` can be a 2-tuple ``('filename', fileobj)``, 3-tuple ``('filename', fileobj, 'content_type')`` 15 or a 4-tuple ``('filename', fileobj, 'content_type', custom_headers)``, where ``'content-type'`` is a string 16 defining the content type of the given file and ``custom_headers`` a dict-like object containing additional headers 17 to add for the file. 18 :param auth: (optional) Auth tuple to enable Basic/Digest/Custom HTTP Auth. 19 :param timeout: (optional) How many seconds to wait for the server to send data 20 before giving up, as a float, or a :ref:`(connect timeout, read 21 timeout) <timeouts>` tuple. 22 :type timeout: float or tuple 23 :param allow_redirects: (optional) Boolean. Enable/disable GET/OPTIONS/POST/PUT/PATCH/DELETE/HEAD redirection. Defaults to ``True``. 24 :type allow_redirects: bool 25 :param proxies: (optional) Dictionary mapping protocol to the URL of the proxy. 26 :param verify: (optional) Either a boolean, in which case it controls whether we verify 27 the server's TLS certificate, or a string, in which case it must be a path 28 to a CA bundle to use. Defaults to ``True``. 29 :param stream: (optional) if ``False``, the response content will be immediately downloaded. 30 :param cert: (optional) if String, path to ssl client cert file (.pem). If Tuple, ('cert', 'key') pair. 31 :return: :class:`Response <Response>` object 32 :rtype: requests.Response 33 34 Usage:: 35 36 >>> import requests 37 >>> req = requests.request('GET', 'https://httpbin.org/get') 38 <Response [200]> 39 """ 40 41 # By using the 'with' statement we are sure the session is closed, thus we 42 # avoid leaving sockets open which can trigger a ResourceWarning in some 43 # cases, and look like a memory leak in others. 44 with sessions.Session() as session: 45 return session.request(method=method, url=url, **kwargs)
其中,month主要是用get和postweb
requests模塊發送請求有data、json、params三種攜帶參數的方法。json
params在get請求中使用,data、json在post請求中使用。api
'''
requests--get方法
r=requests.get('https://movie.douban.com/')
print(r.status_code) #查看狀態碼
print(r.text) #查看響應內容
print(r.content.decode('utf-8')) #查看字節流(二進制)
print(r.url) #查看請求地址
print(r.encoding) #查看字符編碼
print(r.cookies) #查看cookie
print(r.headers) #查看請求頭信息
# 當返回的響應格式是json時,能夠用r.json())
# 這裏有兩種方式,一種是直接用請求地址,另外一種是用參數的方式傳遞parames
url='https://movie.douban.com/subject_search?search_text=%E5%93%AA%E5%90%92&cat=1002'
url1='https://movie.douban.com/subject_search?search_text=哪吒&cat=1002'
paydata={'search_text':'哪吒','cat':1002}
r=requests.get(url1,params=paydata)
'''
post方法:安全
def post(url, data=None, json=None, **kwargs): r"""Sends a POST request. :param url: URL for the new :class:`Request` object. :param data: (optional) Dictionary, list of tuples, 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 \*\*kwargs: Optional arguments that ``request`` takes. :return: :class:`Response <Response>` object :rtype: requests.Response """ return request('post', url, data=data, json=json, **kwargs)
url='https://testyyfax8511.yylending.com/' login_heades={ 'Referer': 'https://testyyfax8511.yylending.com/web/login.html?_z=/web/user/index.html', 'user-agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36'} login_data={ 'account':'18885110001', 'password':'204Ofz2Lrfyng'} test_login=requests.post(url+'node_api/user.do?fn=login',headers=login_heades,data=login_data) print(test_login.text) print(json.dumps(test_login.json(),indent=True,ensure_ascii=False))
當請求參數爲form表單是,用data;當請求參數爲josn格式時,用json;服務器
當響應格式是json時,用json查看會更合適(查看響應字段的時候,用json取值更方便)cookie
request中超時狀況的處理
#客戶端發送到服務端的請求,當服務端響應的時間比較長是,能夠加上等待時間 r=requests.get('http://www.baidu.com',timeout=6)
requests中安全證書的處理思路
當請求須要安全證書的時候,能夠經過忽略安全證書的方式處理或者帶上安全證書的路徑,推薦用忽略的方式。session
r=requests.get('http://www.baidu.com',verify=False)
requests中cookie的處理
第一種方法是直接在請求中帶上具體的cookie的值;
第二種方法,能夠寫一個方法,返回登陸後的cookie,而後在須要cookies的接口中帶上
requests中對token的處理
處理方法同cookie是同樣的思路
requests對session會話對象的處理??
session,保存了客戶端與服務器之間的通訊信息。可以跨請求保持某些參數,好比它會在同一個session實例發出的全部請求之間保持cookie