首頁 >後端開發 >Python教學 >在 Python 請求庫中使用 XML

在 Python 請求庫中使用 XML

DDD
DDD原創
2024-12-30 08:31:29245瀏覽

Working with XML in Python Requests library

什麼是 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中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn