什么是 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中文网其他相关文章!