国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python開發(fā)的HTTP庫requests詳解

2020-02-16 02:09:50
字體:
供稿:網(wǎng)友

Requests 是使用 Apache2 Licensed 許可證的 基于Python開發(fā)的HTTP 庫,其在Python內(nèi)置模塊的基礎(chǔ)上進(jìn)行了高度的封裝,從而使得Pythoner進(jìn)行網(wǎng)絡(luò)請(qǐng)求時(shí),變得美好了許多,使用Requests可以輕而易舉的完成瀏覽器可有的任何操作。

1. GET請(qǐng)求

 # 1、無參數(shù)實(shí)例   import requests   ret = requests.get('https://github.com/timeline.json')   print(ret.url) print(ret.text) # 2、有參數(shù)實(shí)例   import requests   payload = {'key1': 'value1', 'key2': 'value2'} ret = requests.get("http://httpbin.org/get", params=payload)   print(ret.url) print(ret.text)

2. POST請(qǐng)求

 # 1、基本POST實(shí)例   import requests   payload = {'key1': 'value1', 'key2': 'value2'} ret = requests.post("http://httpbin.org/post", data=payload)   print(ret.text)     # 2、發(fā)送請(qǐng)求頭和數(shù)據(jù)實(shí)例   import requests import json   url = 'https://api.github.com/some/endpoint' payload = {'some': 'data'} headers = {'content-type': 'application/json'}   ret = requests.post(url, data=json.dumps(payload), headers=headers)   print(ret.text) print(ret.cookies)

3. 其它請(qǐng)求

 requests.get(url, params=None, **kwargs) requests.post(url, data=None, json=None, **kwargs) requests.put(url, data=None, **kwargs) requests.head(url, **kwargs) requests.delete(url, **kwargs) requests.patch(url, data=None, **kwargs) requests.options(url, **kwargs)   # 以上方法均是在此方法的基礎(chǔ)上構(gòu)建 requests.request(method, url, **kwargs)

4. 請(qǐng)求參數(shù)

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': file-tuple}``) for multipart encoding upload.    ``file-tuple`` can be a 2-tuple ``('filename', fileobj)``, 3-tuple ``('filename', fileobj, 'content_type')``    or a 4-tuple ``('filename', fileobj, 'content_type', custom_headers)``, where ``'content-type'`` is a string    defining the content type of the given file and ``custom_headers`` a dict-like object containing additional headers    to add for the file.  :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]>  """            
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 武义县| 康马县| 馆陶县| 定西市| 布尔津县| 疏附县| 无锡市| 安西县| 信丰县| 定边县| 承德市| 崇阳县| 青田县| 军事| 辽宁省| 寿阳县| 长阳| 白河县| 深圳市| 忻城县| 连江县| 麟游县| 长宁区| 夏河县| 明溪县| 高安市| 罗田县| 米脂县| 漳州市| 湘阴县| 黔西县| 莫力| 平利县| 通城县| 黑龙江省| 许昌市| 锦屏县| 伊宁市| 泰顺县| 鄂州市| 岳阳县|