日韩成人免费在线_国产成人一二_精品国产免费人成电影在线观..._日本一区二区三区久久久久久久久不

當前位置:首頁 > 科技  > 軟件

揭秘Python中的JSON數(shù)據(jù)格式與Requests模塊

來源: 責編: 時間:2024-04-15 18:11:02 200觀看
導讀引言:JSON數(shù)據(jù)格式和Requests模塊在現(xiàn)代編程中扮演著不可或缺的角色。JSON作為一種輕量級的數(shù)據(jù)交換格式,廣泛應用于Web服務之間的數(shù)據(jù)傳輸;而Requests庫則是Python中最流行的HTTP客戶端庫,用于發(fā)起HTTP請求并與服務器交

引言:

Ger28資訊網(wǎng)——每日最新資訊28at.com

JSON數(shù)據(jù)格式和Requests模塊在現(xiàn)代編程中扮演著不可或缺的角色。JSON作為一種輕量級的數(shù)據(jù)交換格式,廣泛應用于Web服務之間的數(shù)據(jù)傳輸;而Requests庫則是Python中最流行的HTTP客戶端庫,用于發(fā)起HTTP請求并與服務器交互。今天,我們將通過10個精選的代碼示例,一同深入了解這兩個重要工具的使用。Ger28資訊網(wǎng)——每日最新資訊28at.com

1.創(chuàng)建并解析JSON數(shù)據(jù)

import json# 創(chuàng)建JSON數(shù)據(jù)data = {    "name": "John",    "age": 30,    "city": "New York"}json_data = json.dumps(data)  # 將Python對象轉(zhuǎn)換為JSON字符串print(json_data)  # 輸出:{"name": "John", "age": 30, "city": "New York"}# 解析JSON數(shù)據(jù)json_string = '{"name": "Jane", "age": 28, "city": "San Francisco"}'parsed_data = json.loads(json_string)  # 將JSON字符串轉(zhuǎn)換為Python字典print(parsed_data)  # 輸出:{'name': 'Jane', 'age': 28, 'city': 'San Francisco'}

2.使用Requests發(fā)送GET請求Ger28資訊網(wǎng)——每日最新資訊28at.com

import requestsresponse = requests.get('https://api.github.com')print(response.status_code)  # 輸出HTTP狀態(tài)碼,如:200print(response.json())  # 輸出響應體內(nèi)容(假設響應是JSON格式)# 保存完整的響應信息with open('github_response.json', 'w') as f:    json.dump(response.json(), f)

3.發(fā)送帶參數(shù)的GET請求

params = {'q': 'Python requests', 'sort': 'stars'}response = requests.get('https://api.github.com/search/repositories', params=params)repos = response.json()['items']for repo in repos[:5]:  # 打印前5個搜索結(jié)果    print(repo['full_name'])

4.發(fā)送POST請求

payload = {'key1': 'value1', 'key2': 'value2'}headers = {'Content-Type': 'application/json'}response = requests.post('http://httpbin.org/post', jsnotallow=payload, headers=headers)print(response.json())

5.設置超時時間

requests.get('http://example.com', timeout=5)  # 設置超時時間為5秒

6.處理Cookies

# 保存cookiesresponse = requests.get('http://example.com')cookies = response.cookies# 發(fā)送帶有cookies的請求requests.get('http://example.com', cookies=cookies)

7.自定義HTTP頭部信息

headers = {'User-Agent': 'My-Custom-UA'}response = requests.get('http://httpbin.org/headers', headers=headers)print(response.text)

8.下載文件

url = 'https://example.com/image.jpg'response = requests.get(url)# 寫入本地文件with open('image.jpg', 'wb') as f:    f.write(response.content)

9.處理身份驗證

from requests.auth import HTTPBasicAuthresponse = requests.get('https://example.com/api', auth=HTTPBasicAuth('username', 'password'))

10.重試機制

from requests.adapters import HTTPAdapterfrom requests.packages.urllib3.util.retry import Retry# 創(chuàng)建一個重試策略retry_strategy = Retry(    total=3,    status_forcelist=[429, 500, 502, 503, 504],    backoff_factor=1,)# 添加重試策略到適配器adapter = HTTPAdapter(max_retries=retry_strategy)# 將適配器添加到會話session = requests.Session()session.mount('http://', adapter)session.mount('https://', adapter)response = session.get('https://example.com')

結(jié)語:

通過上述10個Python中JSON數(shù)據(jù)格式與Requests模塊的實戰(zhàn)示例,相信您對它們的使用有了更為深入的理解。熟練掌握這兩種工具將極大提升您在Web開發(fā)、API調(diào)用等方面的生產(chǎn)力。請持續(xù)關注我們的公眾號,獲取更多Python和其他編程主題的精彩內(nèi)容!Ger28資訊網(wǎng)——每日最新資訊28at.com

本文鏈接:http://www.www897cc.com/showinfo-26-83616-0.html揭秘Python中的JSON數(shù)據(jù)格式與Requests模塊

聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。郵件:2376512515@qq.com

上一篇: C# 中的 HTTP 請求

下一篇: C++中的顯式虛函數(shù)重載:override與final詳解

標簽:
  • 熱門焦點
Top 主站蜘蛛池模板: 和林格尔县| 南皮县| 望城县| 依兰县| 平阴县| 射阳县| 凌海市| 彰化市| 华宁县| 扶余县| 崇信县| 和平县| 保德县| 汝州市| 五原县| 中牟县| 青河县| 乡宁县| 金乡县| 西乌| 黎川县| 从化市| 南木林县| 宁晋县| 喀喇沁旗| 苏州市| 鄂托克旗| 武邑县| 诏安县| 广灵县| 银川市| 营山县| 潍坊市| 元氏县| 壤塘县| 杭锦后旗| 会理县| 星座| 北碚区| 临漳县| 沂源县|