日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

Python requests介绍之接口介绍

發布時間:2023/12/10 python 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python requests介绍之接口介绍 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Python requests介紹

引用官網介紹

Requests 唯一的一個非轉基因的 Python HTTP 庫,人類可以安全享用。

Requests 允許你發送純天然,植物飼養的 HTTP/1.1 請求,無需手工勞動。你不需要手動為 URL 添加查詢字串,也不需要對 POST 數據進行表單編碼。Keep-alive 和 HTTP 連接池的功能是 100% 自動化的,一切動力都來自于根植在 Requests 內部的?urllib3。

功能特性

Requests 完全滿足今日 web 的需求。

  • Keep-Alive & 連接池
  • 國際化域名和 URL
  • 帶持久 Cookie 的會話
  • 瀏覽器式的 SSL 認證
  • 自動內容解碼
  • 基本/摘要式的身份認證
  • 優雅的 key/value Cookie
  • 自動解壓
  • Unicode 響應體
  • HTTP(S) 代理支持
  • 文件分塊上傳
  • 流下載
  • 連接超時
  • 分塊請求
  • 支持?.netrc

Requests 支持 Python 2.6—2.7以及3.3—3.7,而且能在 PyPy 下完美運行。

''' requests 接口介紹 '''

#-*-coding:utf-8-*- # Time:2017/10/12 22:54 # Author:YangYangJun#開始 requests學習之旅,這里直接在代碼編寫文字了。#requests 是python的第三方庫,所以這里需要安裝# 直接在命令行窗口執行 pip install requests 即可#如果提示安裝失敗,可以參看提示,安裝必備軟件然后重新安裝即可#安裝成功后 即可直接導入import requests#如上不報錯即可#參考資料#訪問網址 http://cn.python-requests.org/zh_CN/latest/ 即可查看官方中文文檔#或是查看源代碼 https://github.com/requests/requests#也可到 自己的安裝目錄下查找 D:/SProgram/Python/Lib/site-packages/requests/api.py 等文件查看介紹''' requests 接口介紹 ''' # 這部分文檔包含了 Requests 所有的接口。對于 Requests 依賴的外部庫部分,我們在這里介紹最重要的部分,并提供了規范文檔的鏈接。 # # 主要接口 # Requests 所有的功能都可以通過以下 7 個方法訪問。它們全部都會返回一個 Response 對象的實例。# 1、request()方法 # requests.request(method, url, **kwargs) Constructs and sends a Request. 構造和發送請求。 # 這里的參數 method 、 url 是必選參數,**kwargs 是可選參數,是 可變關鍵字參數,Dictionary 類型 ''' 參數: method -- method for the new Request object. 新請求對象的方法 url -- URL for the new Request object. 新的請求對象的URL params -- (optional) Dictionary or bytes to be sent in the query string for the Request. (可選)在查詢字符串中為請求發送的字典或字節。 data -- (optional) Dictionary or list of tuples [(key, value)] (will be form-encoded), bytes, or file-like object to send in the body of the Request. (可選)字典或元組列表[(鍵,值)](將以表單編碼)、字節或類似文件的對象發送到請求體中。 json -- (optional) json data to send in the body of the Request. (可選)json數據發送到請求體中。 headers -- (optional) Dictionary of HTTP Headers to send with the Request. (可選)HTTP報頭字典,以發送請求。 cookies -- (optional) Dict or CookieJar object to send with the Request. (可選)命令或CookieJar對象發送請求。 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. 438/5000 (可選)“名稱”的字典:文件- like- object(或{' name ':file - tuple}),用于多部分編碼上傳。文件- tuple可以是一個2元組(文件名,fileobj), 3元組(文件名,fileobj,' content_type ')或一個4元組(' filename ',fileobj,' content_type ',custom_headers), 其中' content-type '是一個字符串,它定義了給定文件的內容類型和custom_headers,其中包含了一個類似于dict的對象,其中包含了添加文件的附加頭文件。auth -- (optional) Auth tuple to enable Basic/Digest/Custom HTTP Auth. (可選)Auth tuple來啟用Basic /Digest /自定義HTTP Auth。 timeout (float or tuple) -- (optional) How many seconds to wait for the server to send data before giving up, as a float, or a (connect timeout, read timeout) tuple. (可選)在放棄之前,等待服務器發送數據的時間是多少秒,作為一個浮點數,或者是一個(連接超時,讀超時)元組。 allow_redirects (bool) -- (optional) Boolean. Enable/disable GET/OPTIONS/POST/PUT/PATCH/DELETE/HEAD redirection. Defaults to True. (可選)布爾。啟用/禁用GET /選項/ POST / PUT /補丁/刪除/頭重定向。默認值為True。 proxies -- (optional) Dictionary mapping protocol to the URL of the proxy. (可選)字典映射協議到代理的URL。 verify -- (optional) Either a boolean, in which case it controls whether we verify the server's TLS certificate, or a string, in which case it must be a path to a CA bundle to use. Defaults to True. (可選)一個布爾值,在這種情況下,它控制我們是否驗證服務器的TLS證書,或者是一個字符串,在這種情況下,它必須是一個到CA包使用的路徑。默認值為True。 stream -- (optional) if False, the response content will be immediately downloaded. (可選)如果錯誤,將立即下載響應內容。 cert -- (optional) if String, path to ssl client cert file (.pem). If Tuple, ('cert', 'key') pair. (可選)如果字符串,路徑到ssl客戶機cert文件(. pem)。如果是Tuple(' cert ',' key ')對。 '''# request()方法實例 req = requests.request('GET', 'http://httpbin.org/get')print req # 得到 <Response [200]># 2、head()方法# requests.head(url, **kwargs) # Sends a HEAD request. 發送一個HEAD 請求。# Optional arguments that ``request`` takes. 請求獲取的可選參數。 r"""Sends a HEAD request.:param url: URL for the new :class:`Request` object.:param \*\*kwargs: Optional arguments that ``request`` takes. :return: :class:`Response <Response>` object :rtype: requests.Response""" # head()方法實例 req = requests.head('http://httpbin.org/get')print req # 得到 <Response [200]># 3、get()方法# requests.get(url, params=None, **kwargs)[源代碼] # Sends a GET request. # # 參數: # url -- URL for the new Request object. # params -- (optional) Dictionary or bytes to be sent in the query string for the Request. # **kwargs -- Optional arguments that request takes. # 返回: # Response object # # 返回類型: # requests.Response# get()方法實例 req = requests.get('http://httpbin.org/get')print req # 得到 <Response [200]># 4、post()方法# requests.post(url, data=None, json=None, **kwargs)[源代碼] # Sends a POST request. # # 參數: # url -- URL for the new Request object. # data -- (optional) Dictionary (will be form-encoded), bytes, or file-like object to send in the body of the Request. # json -- (optional) json data to send in the body of the Request. # **kwargs -- Optional arguments that request takes. # 返回: # Response object # # 返回類型: # requests.Response# post()方法實例 req = requests.post('http://httpbin.org/get')print req # 得到 <Response [405]># 5、put()方法# requests.put(url, data=None, **kwargs)[源代碼] # Sends a PUT request. # # 參數: # url -- URL for the new Request object. # data -- (optional) Dictionary (will be form-encoded), bytes, or file-like object to send in the body of the Request. # json -- (optional) json data to send in the body of the Request. # **kwargs -- Optional arguments that request takes. # 返回: # Response object # # 返回類型: # requests.Response# 6、patch()方法# requests.patch(url, data=None, **kwargs)[源代碼] # Sends a PATCH request. # # 參數: # url -- URL for the new Request object. # data -- (optional) Dictionary (will be form-encoded), bytes, or file-like object to send in the body of the Request. # json -- (optional) json data to send in the body of the Request. # **kwargs -- Optional arguments that request takes. # 返回: # Response object # # 返回類型: # requests.Response# 7、delete()方法# requests.delete(url, **kwargs)[源代碼] # Sends a DELETE request. # # 參數: # url -- URL for the new Request object. # **kwargs -- Optional arguments that request takes. # 返回: # Response object # # 返回類型: # requests.Response #

?

轉載于:https://www.cnblogs.com/BlueSkyyj/p/7652860.html

總結

以上是生活随笔為你收集整理的Python requests介绍之接口介绍的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。