什麼是 XML? XML 是指可擴展標記語言,它需要儲存結構化資料並對任何項目進行分組。在 XML 標記語言中,您可以建立具有任何名稱的標籤。最受歡迎的 XML 範例 - 網站地圖和 RSS 提要。
XML 檔案範例:
<breakfast_menu> <food> <name>Belgian Waffles</name> <price>.95</price> <description>Two of our famous Belgian Waffles with plenty of real maple syrup</description> <calories>650</calories> </food> <food> <name>Strawberry Belgian Waffles</name> <price>.95</price> <description>Light Belgian waffles covered with strawberries and whipped cream</description> <calories>900</calories> </food> <food> <name>Berry-Berry Belgian Waffles</name> <price>.95</price> <description>Light Belgian waffles covered with an assortment of fresh berries and whipped cream</description> <calories>900</calories> </food> <food> <name>French Toast</name> <price>.50</price> <description>Thick slices made from our homemade sourdough bread</description> <calories>600</calories> </food> <food> <name>Homestyle Breakfast</name> <price>.95</price> <description>Two eggs, bacon or sausage, toast, and our ever-popular hash browns</description> <calories>950</calories> </food> </breakfast_menu>
在這個範例中,檔案包含 breakfast_menu 全域標籤,其中包含食物類別,每個食物類別包括名稱、價格、描述和卡路里標籤。
現在我們開始學習如何使用 XML 和 Python 請求函式庫。首先我們要準備好工作環境。
要建立新專案和虛擬環境,請安裝 python3-virtualenv 套件。它需要每個項目的分離要求。在 Debian/Ubuntu 中安裝:
sudo apt install python3 python3-virtualenv -y
建立專案資料夾:
mkdir my_project cd my_project
使用 env 命名的資料夾建立 Python 虛擬環境:
python3 -m venv env
啟動虛擬環境:
source env/bin/activate
安裝 PIP 的依賴項:
pip3 install requests
讓我們開始寫程式碼吧。
建立 main.py 檔案並在下面插入程式碼:
import requests import xml.etree.ElementTree as ET request = requests.get('https://www.w3schools.com/xml/simple.xml') root = ET.fromstring(request.content) for item in root.iter('*'): print(item.tag)
此程式碼片段幫助我們找到所有內部標籤。
此程式碼的輸出:
(env) user@localhost:~/my_project$ python3 main.py breakfast_menu food name price description calories food name price description calories food name price description calories food name price description calories food name price description calories
現在我們正在編寫從內部元素獲取值的程式碼。開啟 main.py 檔案並將先前的程式碼替換為:
import requests import xml.etree.ElementTree as ET request = requests.get('https://www.w3schools.com/xml/simple.xml') root = ET.fromstring(request.content) for item in root.iterfind('food'): print(item.findtext('name')) print(item.findtext('price')) print(item.findtext('description')) print(item.findtext('calories'))
我們收到了下一個結果:
(env) user@localhost:~/my_project$ python3 main.py Belgian Waffles .95 Two of our famous Belgian Waffles with plenty of real maple syrup 650 Strawberry Belgian Waffles .95 Light Belgian waffles covered with strawberries and whipped cream 900 Berry-Berry Belgian Waffles .95 Light Belgian waffles covered with an assortment of fresh berries and whipped cream 900 French Toast .50 Thick slices made from our homemade sourdough bread 600 Homestyle Breakfast .95 Two eggs, bacon or sausage, toast, and our ever-popular hash browns 950
最後一步,我們美化輸出資料使其更易於閱讀:
import requests import xml.etree.ElementTree as ET request = requests.get('https://www.w3schools.com/xml/simple.xml') root = ET.fromstring(request.content) for item in root.iterfind('food'): print('Name: {}. Price: {}. Description: {}. Calories: {}'.format(item.findtext('name'), item.findtext('price'), item.findtext('description'), item.findtext('calories')))
此處輸出:
(env) user@localhost:~/my_project$ python3 main.py Name: Belgian Waffles. Price: .95. Description: Two of our famous Belgian Waffles with plenty of real maple syrup. Calories: 650 Name: Strawberry Belgian Waffles. Price: .95. Description: Light Belgian waffles covered with strawberries and whipped cream. Calories: 900 Name: Berry-Berry Belgian Waffles. Price: .95. Description: Light Belgian waffles covered with an assortment of fresh berries and whipped cream. Calories: 900 Name: French Toast. Price: .50. Description: Thick slices made from our homemade sourdough bread. Calories: 600 Name: Homestyle Breakfast. Price: .95. Description: Two eggs, bacon or sausage, toast, and our ever-popular hash browns. Calories: 950
來源材料:
XML 檔案範例取自 W3Schools。
我的貼文有幫助嗎?您可以在 Patreon 上支持我。
以上是在 Python 請求庫中使用 XML的詳細內容。更多資訊請關注PHP中文網其他相關文章!

文章討論了從數學模塊到圓形數量向下的Python的Floor()函數,將其與Round()函數進行比較,及其與負數的使用。

本文討論了Python中的可變範圍,詳細介紹了本地和全局範圍,以及範圍對可變可訪問性的影響。它突出了常見的錯誤,以避免有效的代碼管理。

Python需要縮進來定義代碼塊,與使用牙套的其他語言不同。不正確的凹痕會導致語法錯誤,邏輯問題和可讀性問題。 IDE和Linters之類的工具有助於管理它。

本文討論了動態鍵入的語言,其中類型檢查在運行時發生,與靜態鍵入語言形成鮮明對比。它檢查了靈活性和快速發展等優勢,並影響軟件開發和維護

Python主要是一種解釋的語言,影響其性能,開發速度和跨平台兼容性。本文討論了Python解釋過程及其對這些方面的影響的細微差別。

Python腳本在Unix系統上無法運行的原因包括:1)權限不足,使用chmod xyour_script.py賦予執行權限;2)Shebang行錯誤或缺失,應使用#!/usr/bin/envpython;3)環境變量設置不當,可打印os.environ調試;4)使用錯誤的Python版本,可在Shebang行或命令行指定版本;5)依賴問題,使用虛擬環境隔離依賴;6)語法錯誤,使用python-mpy_compileyour_script.py檢測。

使用Python數組比列表更適合處理大量數值數據。 1)數組更節省內存,2)數組對數值運算更快,3)數組強制類型一致性,4)數組與C語言數組兼容,但在靈活性和便捷性上不如列表。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

WebStorm Mac版
好用的JavaScript開發工具

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

Atom編輯器mac版下載
最受歡迎的的開源編輯器

Dreamweaver CS6
視覺化網頁開發工具