Apakah itu XML? XML bermaksud Extensible Markup Language, yang memerlukan untuk menyimpan data berstruktur dan mengumpulkan sebarang item. Dalam bahasa markup XML, anda boleh membuat teg dengan sebarang nama. Contoh XML yang paling popular - Peta Laman dan suapan RSS.
Contoh fail 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>
Dalam contoh ini, fail mengandungi teg global breakfast_menu, yang termasuk kategori makanan dan setiap kategori makanan termasuk nama, harga, keterangan dan teg kalori.
Sekarang kita mula belajar cara bekerja dengan perpustakaan XML dan Python Requests. Mula-mula kita perlu menyediakan persekitaran kerja kita.
Untuk mencipta projek baharu dan persekitaran maya pasang pakej python3-virtualenv. Ia memerlukan keperluan pengasingan setiap projek. Pemasangan dalam Debian/Ubuntu:
sudo apt install python3 python3-virtualenv -y
Buat folder projek:
mkdir my_project cd my_project
Buat persekitaran maya Python dengan folder bernama env:
python3 -m venv env
Aktifkan persekitaran maya:
source env/bin/activate
Pasang kebergantungan PIP:
pip3 install requests
Mari mulakan penulisan kod.
Buat fail main.py dan masukkan kod di bawah:
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)
Coretan kod ini membantu kami mencari semua teg dalaman.
Keluaran kod ini:
(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
Kini kami menulis kod untuk mendapatkan nilai daripada elemen dalaman. Buka fail main.py dan gantikan kod sebelumnya dengan ini:
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'))
Kami menerima keputusan seterusnya:
(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
Pada langkah terakhir kami mempercantikkan data output untuk memudahkan pembacaan:
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')))
Di sini output:
(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
Bahan sumber:
Contoh fail XML yang diambil daripada W3Schools.
Adakah siaran saya berguna? Anda boleh menyokong saya di Patreon.
Atas ialah kandungan terperinci Bekerja dengan XML dalam perpustakaan Permintaan Python. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!