1. 引言1.1. 什么是HTTP請求?HTTP(超文本傳輸協(xié)議)是一種用于 Web 客戶端(如瀏覽器)和服務器之間通信的協(xié)議。HTTP 請求是客戶端發(fā)送的用于向服務器請求信息的消息。它通常包括 HTTP 方法(GET、POST、PUT、DELETE 等)、標頭等詳細信息,有時還包括請求正文。 1.2. 為什么要在 Python 中發(fā)出 HTTP 請求?在 Python 中發(fā)出 HTTP 請求的原因有很多: - 數(shù)據(jù)檢索:您可以從各種 Web API 獲取數(shù)據(jù),以集成到 Python 應用程序中或執(zhí)行數(shù)據(jù)分析。
- 網(wǎng)頁抓?。?/span>您可以從網(wǎng)站抓取數(shù)據(jù)用于各種目的,例如數(shù)據(jù)挖掘、研究或內(nèi)容聚合。
- API集成:許多服務都提供 API 以編程方式進行交互;Python 可用于與這些 API 進行通信。
- 測試和調(diào)試:您可以通過以編程方式發(fā)送請求和檢查響應來測試和調(diào)試 Web 應用程序。
現(xiàn)在,讓我們探討一下可用于在 Python 中發(fā)出 HTTP 請求的不同技術和庫。 2. 請求庫2.1. 安裝requests 庫是 Python 中用于發(fā)出 HTTP 請求的最流行的庫之一。要安裝它,您可以使用 pip: pip install requests
2.2. 發(fā)出 GET 請求import requests# Sending a GET requestresponse = requests.get('https://jsonplaceholder./posts/1')# Checking the response status codeif response.status_code == 200: # Printing the response content (JSON data in this case) print(response.json())else: print('Request failed with status code:', response.status_code)
2.3. 發(fā)送查詢參數(shù)可以使用 params 參數(shù)隨 GET 請求發(fā)送查詢參數(shù): import requests# Sending a GET request with query parametersparams = {'key1': 'value1', 'key2': 'value2'}response = requests.get('https:///api/resource', params=params)
2.4. 處理響應響應對象提供了各種屬性和方法來處理響應數(shù)據(jù): import requestsresponse = requests.get('https://jsonplaceholder./posts/1')# Accessing response content as textcontent = response.text# Accessing response content as bytescontent_bytes = response.content# Parsing JSON responsedata = response.json()# Accessing response headersheaders = response.headers# Checking if a specific header existsif 'Content-Type' in headers: content_type = headers['Content-Type']# Checking the response status codestatus_code = response.status_code
2.5. 發(fā)出 POST 請求發(fā)出 POST 請求就像發(fā)出 GET 請求一樣簡單: import requests# Data to be sent as JSON in the request bodydata = {'key1': 'value1', 'key2': 'value2'}response = requests.post('https:///api/endpoint', json=data)
2.6. 會話管理您可以使用會話在多個請求中保留某些參數(shù),例如 Cookie 和標頭: import requests# Create a sessionsession = requests.Session()# Set headers that will be included in all requests made with this sessionsession.headers.update({'User-Agent': 'MyApp'})# Send multiple requests within the same sessionresponse1 = session.get('https:///endpoint1')response2 = session.get('https:///endpoint2')
2.7. 處理標頭您可以為每個請求自定義標頭: import requests# Custom headers for the requestheaders = {'Authorization': 'Bearer my_token', 'User-Agent': 'MyApp'}response = requests.get('https:///api/resource', headers=headers)
2.8. 處理 Cookie您可以使用 cookies 屬性處理 cookie: import requests# Send a request and get cookies from the responseresponse = requests.get('https://')cookies = response.cookies# Use cookies in subsequent requestsresponse2 = requests.get('https:///secure', cookies=cookies)
2.9. 錯誤處理使用異常優(yōu)雅地處理錯誤: import requeststry: response = requests.get('https:///nonexistent') response.raise_for_status() # Raise an exception for non-2xx responsesexcept requests.exceptions.HTTPError as e: print('HTTP error occurred:', e)except requests.exceptions.RequestException as e: print('Request error occurred:', e)
注意:單擊此處了解有關 Python 中請求的更多信息 3. http.client 庫http.client 模塊是 Python 標準庫的一部分,與請求庫相比,它提供了一種較低級別的 HTTP 請求方式。 3.1. 基本用法import http.client# Create an HTTP connectionconn = http.client.HTTPSConnection('')# Send a GET requestconn.request('GET', '/')# Get the responseresponse = conn.getresponse()# Read and print the response contentdata = response.read()print(data.decode('utf-8'))
3.2. 發(fā)出 GET 請求import http.clientconn = http.client.HTTPSConnection('')conn.request('GET', '/')response = conn.getresponse()
3.3. 處理響應import http.clientconn = http.client.HTTPSConnection('')conn.request('GET', '/')response = conn.getresponse()# Read response statusstatus_code = response.statusstatus_reason = response.reason# Read response headersheaders = response.getheaders()# Read response contentdata = response.read()
3.4. 發(fā)出 POST 請求import http.clientimport jsondata = {'key1': 'value1', 'key2': 'value2'}payload = json.dumps(data)conn = http.client.HTTPSConnection('')headers = {'Content-type': 'application/json'}conn.request('POST', '/api/endpoint', body=payload, headers=headers)response = conn.getresponse()
3.5. 錯誤處理import http.clienttry: conn = http.client.HTTPSConnection('') conn.request('GET', '/nonexistent') response = conn.getresponse() if response.status != 200: print('Request failed with status code:', response.status)except http.client.HTTPException as e: print('HTTP exception occurred:', e)except Exception as e: print('An error occurred:', e)finally: conn.close()
注意:單擊此處了解有關 Python 中 http.client 的更多信息 4. urllib 庫urllib 模塊是 Python 標準庫的一部分,提供了一種發(fā)出 HTTP 請求的簡單方法。 4.1. 發(fā)出 GET 請求import urllib.requestresponse = urllib.request.urlopen('https://')html = response.read()print(html.decode('utf-8'))
4.2. 發(fā)送查詢參數(shù)import urllib.parseimport urllib.requestparams = {'key1': 'value1', 'key2': 'value2'}url = 'https:///api/resource?' + urllib.parse.urlencode(params)response = urllib.request.urlopen(url)
4.3. 處理響應import urllib.requestresponse = urllib.request.urlopen('https://')content = response.read()# Get response headersheaders = response.getheaders()# Get response status codestatus_code = response.getcode()
4.4. 發(fā)出 POST 請求import urllib.requestimport urllib.parseimport jsondata = {'key1': 'value1', 'key2': 'value2'}payload = json.dumps(data).encode('utf-8')req = urllib.request.Request('https:///api/endpoint', data=payload, method='POST')req.add_header('Content-Type', 'application/json')response = urllib.request.urlopen(req)
4.5. 錯誤處理import urllib.requestimport urllib.errortry: response = urllib.request.urlopen('https:///nonexistent')except urllib.error.HTTPError as e: print('HTTP error occurred:', e)except urllib.error.URLError as e: print('URL error occurred:', e)
注意:單擊此處了解有關 Python 中 urllib 的更多信息
|