欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python HTTP庫 requests 的簡單使用詳情

 更新時間:2021年09月29日 11:15:45   作者:tigeriaf  
requests是Python的一個HTTP客戶端庫,基于urllib標(biāo)準(zhǔn)庫,在urllib標(biāo)準(zhǔn)庫的基礎(chǔ)上做了高度封裝,因此更加簡潔好用,下面就由小編來給大家詳細(xì)介紹吧,需要的朋友可以參考下

requests庫實現(xiàn)了HTTP協(xié)議中絕大部分功能,提供了Keep-Alive、連接池、Cookie持久化、HTTP(S)代理支持、連接超時等很多功能特性,最重要的是它同時支持Python2和ython3,而且能在PyPy下完美運(yùn)行。

使用前需要使用pip install requests命令進(jìn)行安裝。

1、簡單使用

res = requests.get("http://httpbin.org/get")
# 狀態(tài)碼
print(res.status_code)
# 響應(yīng)頭
print(res.headers["Content-Type"], res.headers["Server"])
# 響應(yīng)內(nèi)容
print(res.text)

執(zhí)行結(jié)果如下:

200
application/json gunicorn/19.9.0
{
  "args": {},
  "headers": {
    "Accept": "*/*",
    "Accept-Encoding": "gzip, deflate",
    .......
  },
  "origin": "xxx.xxx.xx.xx", 
  "url": http://httpbin.org/get
}

另外,http請求還有很多類型,比如POST、PUT、DELETE、HEAD、OPTIONS。requests也都可以以簡單的方式實現(xiàn)。

res = requests.post("http://httpbin.org/post")
res = requests.put("http://httpbin.org/put")
res = requests.delete("http://httpbin.org/delete")
res = requests.head("http://httpbin.org/get")
res = requests.options("http://httpbin.org/get")

由此看來,使用requests庫確實簡單方便。

2、構(gòu)建請求查詢參數(shù)

很多請求都需要在URL中傳遞參數(shù),我們可以用字典來構(gòu)建查詢參數(shù),使用params參數(shù)在URL中添加參數(shù)。

payload = {"wd": "test"}
res = requests.get("https://www.baidu.com/", params=payload)
print(res.url)

運(yùn)行結(jié)果如下:

https://www.baidu.com/?wd=test

3、構(gòu)建請求頭Headers

requests可以在請求中很簡單的指定請求頭的Headers信息,直接傳遞一個字典給參數(shù)headers即可。

headers = {"user-agent": "Mozilla/5.0", "cookies": "xxx"}
res = requests.get("https://www.baidu.com/", headers=headers)

4、構(gòu)建POST請求數(shù)據(jù)

requests可以非常方便的構(gòu)建POST請求需要的數(shù)據(jù)。如果服務(wù)端接收的的數(shù)據(jù)是表單數(shù)據(jù),可以使用參數(shù)data上送,如果接收的是json格式的數(shù)據(jù),則可以使用json參數(shù)上送。

4.1 表單數(shù)據(jù)

import requests

data = {"key1": "value1", "key2": "value2"}
res = requests.post("http://httpbin.org/post", data=data)
print(res.text)

運(yùn)行結(jié)果如下:

{
  "args": {},
  "data": "",
  "files": {},
  "form": {
    "key1": "value1",
    "key2": "value2"
  },
  "headers": {
    "Accept": "*/*",
    "Accept-Encoding": "gzip, deflate",
    "Content-Length": "23",
    "Content-Type": "application/x-www-form-urlencoded",
    "Host": "httpbin.org",
    "User-Agent": "python-requests/2.26.0",
    "X-Amzn-Trace-Id": "Root=1-614d7d91-559333ee19237f845026ef37"
  },
  "json": null,
  "origin": "xxx.xxx.xx.xx",
  "url": " }

4.2 json數(shù)據(jù)

import json
import requests

url = "http://httpbin.org/post"
data = {"key": "value"}
data = json.dumps(data)
res = requests.post(url, data=data)
print(res.text)

運(yùn)行結(jié)果如下:

{
  "args": {},
  "data": "{\"key\": \"value\"}",
  "files": {},
  "form": {},
  "headers": {
    "Accept": "*/*",
    "Accept-Encoding": "gzip, deflate",
    "Content-Length": "16",
    "Host": "httpbin.org",
    "User-Agent": "python-requests/2.26.0",
    "X-Amzn-Trace-Id": "Root=1-614d7e91-065887f925dce94d6d03b2e4"
  },
  "json": {
    "key": "value"
  },
  "origin": "xxx.xxx.xx.xx",
  "url": "http://httpbin.org/post"
}

5、獲取響應(yīng)內(nèi)容

使用requests請求處理響應(yīng)體也非常方便靈活,可以使用的屬性有contenttext、json()

content屬性獲取的是byte類型的數(shù)據(jù)。

import requests


res = requests.get("http://httpbin.org/get")
print(res.content)

text屬性獲取的是str類型的數(shù)據(jù)。

import requests


res = requests.get("http://httpbin.org/get")
print(res.text)

如果返回的內(nèi)容是json格式的數(shù)據(jù)時,就可以使用json()方法返回一個經(jīng)過json.loads()處理后的對象。

import requests

url = "http://httpbin.org/post"
res = requests.post(url)
print(res.json())

6、Cookies

如果響應(yīng)中包含了cookie信息,我們可以使用cookies屬性獲取。

res = requests.get("http://httpbin.org/get")
print(res.cookies)

另外還可以使用cookies參數(shù)向服務(wù)端發(fā)送cookies信息。

url = "http://httpbin.org/cookies"
cookies = {"cookies": "xxxxx"}
r = requests.get(url, cookies=cookies)
print(r.text)

7、超時配置

可以利用timeout參數(shù)來配置最大請求時間。

requests.get("https://baidu.com", timeout=0.01)

8、代理

如果需要使用代理,我們可以通過proxies參數(shù)來配置。

import requests

proxies = {
  'http': 'http://175.7.199.202:3256',
  'https': 'http://175.7.199.59:3256',
}

requests.get('http://httpbin.org/get', proxies=proxies)

總結(jié):

到此這篇關(guān)于Python HTTP庫 requests 的簡單使用詳情的文章就介紹到這了,更多相關(guān)Python HTTP庫 requests 的簡單使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • python實現(xiàn)分析apache和nginx日志文件并輸出訪客ip列表的方法

    python實現(xiàn)分析apache和nginx日志文件并輸出訪客ip列表的方法

    這篇文章主要介紹了python實現(xiàn)分析apache和nginx日志文件并輸出訪客ip列表的方法,涉及Python操作日志文件的技巧,非常具有實用價值,需要的朋友可以參考下
    2015-04-04
  • Pycharm插件(Grep Console)自定義規(guī)則輸出顏色日志的方法

    Pycharm插件(Grep Console)自定義規(guī)則輸出顏色日志的方法

    這篇文章主要介紹了Pycharm插件(Grep Console)自定義規(guī)則輸出顏色日志的方法,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-05-05
  • Python Django 添加首頁尾頁上一頁下一頁代碼實例

    Python Django 添加首頁尾頁上一頁下一頁代碼實例

    這篇文章主要介紹了Python Django 添加首頁尾頁上一頁下一頁代碼實例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-08-08
  • Python字符串的全排列算法實例詳解

    Python字符串的全排列算法實例詳解

    這篇文章主要介紹了Python字符串的全排列算法,結(jié)合實例形式較為詳細(xì)的總結(jié)分析了Python字符串全排列的常見操作技巧,需要的朋友可以參考下
    2019-01-01
  • Python urllib.request對象案例解析

    Python urllib.request對象案例解析

    這篇文章主要介紹了Python urllib.request對象案例解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-05-05
  • python繪制春節(jié)煙花的示例代碼

    python繪制春節(jié)煙花的示例代碼

    這篇文章主要介紹了使用python 實現(xiàn)的簡單春節(jié)煙花效果的示例代碼,請注意,運(yùn)行本文的代碼之前,請確保計算機(jī)上已經(jīng)安裝了Pygame庫,需要的朋友可以參考下
    2024-02-02
  • 利用 Python 實現(xiàn)多任務(wù)進(jìn)程

    利用 Python 實現(xiàn)多任務(wù)進(jìn)程

    這篇文章主要介紹如何利用 Python 實現(xiàn)多任務(wù)進(jìn)程,正在執(zhí)行的程序,由程序、數(shù)據(jù)和進(jìn)程控制塊組成,是正在執(zhí)行的程序,程序的一次執(zhí)行過程,是資源調(diào)度的基本單位。下面就來詳細(xì)介紹改內(nèi)容,需要的朋友可以參考一下
    2021-10-10
  • Python使用gluon/mxnet模塊實現(xiàn)的mnist手寫數(shù)字識別功能完整示例

    Python使用gluon/mxnet模塊實現(xiàn)的mnist手寫數(shù)字識別功能完整示例

    這篇文章主要介紹了Python使用gluon/mxnet模塊實現(xiàn)的mnist手寫數(shù)字識別功能,結(jié)合完整實例形式分析了Python調(diào)用gluon/mxnet模塊識別手寫字的具體實現(xiàn)技巧,需要的朋友可以參考下
    2019-12-12
  • Python基礎(chǔ)之常用庫常用方法整理

    Python基礎(chǔ)之常用庫常用方法整理

    這篇文章主要介紹了Python基礎(chǔ)之常用庫常用方法整理,文中有非常詳細(xì)的代碼示例,對正在學(xué)習(xí)python基礎(chǔ)的小伙伴們有非常好的幫助,需要的朋友可以參考下
    2021-04-04
  • Python 實現(xiàn)使用空值進(jìn)行賦值 None

    Python 實現(xiàn)使用空值進(jìn)行賦值 None

    這篇文章主要介紹了Python 實現(xiàn)使用空值進(jìn)行賦值 None,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-03-03

最新評論