Вот XML-документ, который у меня есть:
<product>
<portable_computers category="Tablets" id="1">
<img_main>https://images-na.ssl-images-amazon.com/images/I/51YWYDIYAnL._SL1000_.jpg</img_main>
<name>*NEW*'Microsoft Surface Pro 6 Black 12.3" (2736 x 1824) Touch Screen '
'Tablet with Keyboard, Surface Pen and Mouse - Intel 8th Gen Core i5 '
'(up to 3.40 GHz) - 8GB RAM - 256GB SSD'</name>
<link>https://www.amazon.com/Microsoft-Surface-Screen-Tablet-Keyboard/dp/B07T8DFXZH</link>
<price>
<newprice type="USD">
<currency>USD</currency>
<value>1158.99</value>
<value_tf>897.99</value_tf>
</newprice>
<newprice type="UAH">
<currency>UAH</currency>
<value>28685.0025</value>
</newprice>
</price>
<producer>
<Company>Microsoft</Company>
<CompType>Corp</CompType>
</producer>
</portable_computers>
<portable_computers category="Tablets" id="2">
<img_main>https://images-na.ssl-images-amazon.com/images/I/61ovkSA9p4L._SL1500_.jpg</img_main>
<name>*NEW*'Apple iPad Pro (11-inch, Wi-Fi + Cellular 256GB) - Space Gray '
'(Renewed)'</name>
<link>https://www.amazon.com/Apple-11-inch-Wi-Fi-Cellular-256GB/dp/B07T7KRCD6</link>
<price>
<newprice type="USD">
<currency>USD</currency>
<value>739.99</value>
<value_tf>597.99</value_tf>
</newprice>
<newprice type="UAH">
<currency>UAH</currency>
<value>18314.7525</value>
</newprice>
</price>
<producer>
<Company>Amazon</Company>
<CompType>Ltd</CompType>
</producer>
</portable_computers>
<portable_computers category="Tablets" id="3">
<img_main>https://images-na.ssl-images-amazon.com/images/I/61h6BDl1nTL._SL1500_.jpg</img_main>
<name>*NEW*'Apple iPad Pro 3rd Generation (12.9-inch, Wi-Fi, 64GB) - Space Gray '
'(Renewed)'</name>
<link>https://www.amazon.com/Apple-iPad-Generation-12-9-inch-Wi-Fi/dp/B07T1VVCQ3</link>
<price>
<newprice type="USD">
<currency>USD</currency>
<value>699.00</value>
<value_tf>512.00</value_tf>
</newprice>
<newprice type="UAH">
<currency>UAH</currency>
<value>17300.25</value>
</newprice>
</price>
<producer>
<Company>Amazon</Company>
<CompType>Ltd</CompType>
</producer>
</portable_computers>
<portable_computers category="Tablets" id="4">
<img_main>https://images-na.ssl-images-amazon.com/images/I/71Sqanb5KvL._SL1500_.jpg</img_main>
<name>*NEW*'High Performance RCA 7 Inch 1GB RAM 16G Tablet MTK Quad Core Dual '
'Camera Touch IPS Screen 1024 x 600 WiFi Bluetooth Android 7.0 Black '
'(Renewed)'</name>
<link>https://www.amazon.com/Performance-RCA-Bluetooth-Android-Renewed/dp/B07WDCQNTR</link>
<price>
<newprice type="USD">
<currency>USD</currency>
<value>33.99</value>
<value_tf>20.99</value_tf>
</newprice>
<newprice type="UAH">
<currency>UAH</currency>
<value>841.2525</value>
</newprice>
</price>
<producer>
<Company>Amazon</Company>
<CompType>Ltd</CompType>
</producer>
</portable_computers>
<portable_computers category="Tablets" id="5">
<img_main>https://images-na.ssl-images-amazon.com/images/I/61h6BDl1nTL._SL1500_.jpg</img_main>
<name>*NEW*'Apple iPad Pro 3rd Gen (12.9-inch, Wi-Fi + Cellular, 1TB) - Space '
'Gray (Renewed)'</name>
<link>https://www.amazon.com/Apple-iPad-12-9-inch-Wi-Fi-Cellular/dp/B07VFKM28L</link>
<price>
<newprice type="USD">
<currency>USD</currency>
<value>1049.99</value>
<value_tf>823.99</value_tf>
</newprice>
<newprice type="UAH">
<currency>UAH</currency>
<value>25987.2525</value>
</newprice>
</price>
<producer>
<Company>Amazon</Company>
<CompType>Ltd</CompType>
</producer>
</portable_computers>
<portable_computers category="Tablets" id="6">
<img_main>https://images-na.ssl-images-amazon.com/images/I/71taKQj3uZL._SL1344_.jpg</img_main>
<name>*NEW*'2019 Samsung Galaxy Tab A 10.1-inch Touchscreen (1920x1200) Wi-Fi '
'Tablet Bundle, Exynos 7904A Processor, 2GB RAM, 32GB Memory, '
'BMali-G71 MP2 Graphics, Bluetooth,Tigology Case, Android 9.0 Pie OS'</name>
<link>https://www.amazon.com/2019-Samsung-Galaxy-Tab-Touchscreen/dp/B07VWCSNR9</link>
<price>
<newprice type="USD">
<currency>USD</currency>
<value>249.00</value>
<value_tf>153.00</value_tf>
</newprice>
<newprice type="UAH">
<currency>UAH</currency>
<value>6162.75</value>
</newprice>
</price>
<producer>
<Company>Samsung</Company>
<CompType>Corp</CompType>
</producer>
</portable_computers>
<portable_computers category="Tablets" id="7">
<img_main>https://images-na.ssl-images-amazon.com/images/I/51mAkcVFtVL._SL1024_.jpg</img_main>
<name>*NEW*'Apple iPad Pro (12.9-inch, Wi-Fi, 1TB) - Space Gray (Latest Model) '
'(Renewed)'</name>
<link>https://www.amazon.com/Apple-iPad-Pro-12-9-inch-Wi-Fi/dp/B07NX2L7BY</link>
<price>
<newprice type="USD">
<currency>USD</currency>
<value>999.99</value>
<value_tf>899.99</value_tf>
</newprice>
<newprice type="UAH">
<currency>UAH</currency>
<value>24749.7525</value>
</newprice>
</price>
<producer>
<Company>Amazon</Company>
<CompType>Ltd</CompType>
</producer>
</portable_computers>
<portable_computers category="Tablets" id="8">
<img_main>https://images-na.ssl-images-amazon.com/images/I/71RRIH5eJqL._SL1344_.jpg</img_main>
<name>*NEW*'Samsung Galaxy Tab A 10.1-inch Touchscreen (1920x1200) Wi-Fi Tablet '
'Bundle, Octa-Core 1.6GHz Processor, 2GB RAM, 16GB Memory, 32GB '
'MicroSD Card, Tigology Case, Screen Protector, Android OS'</name>
<link>https://www.amazon.com/Samsung-Galaxy-Tab-Bundle-Case/dp/B07THLXNVW</link>
<price>
<newprice type="USD">
<currency>USD</currency>
<value>229.00</value>
<value_tf>131.00</value_tf>
</newprice>
<newprice type="UAH">
<currency>UAH</currency>
<value>5667.75</value>
</newprice>
</price>
<producer>
<Company>Samsung</Company>
<CompType>Corp</CompType>
</producer>
</portable_computers>
<portable_computers category="Tablets" id="9">
<img_main>https://images-na.ssl-images-amazon.com/images/I/51rCjfNvgwL._SL1000_.jpg</img_main>
<name>*NEW*'Microsoft Surface Pro 6 12.3" (2736 x 1824) Touch Screen - Intel '
'8th Gen Core i5 (up to 3.40 GHz) - 8GB Memory - 256GB SSD - with '
'Keyboard, Surface Pen and Arc Mouse - Black'</name>
<link>https://www.amazon.com/Microsoft-Surface-12-3-Touch-Screen/dp/B07SS4BNB5</link>
<price>
<newprice type="USD">
<currency>USD</currency>
<value>1363.99</value>
<value_tf>1231.99</value_tf>
</newprice>
<newprice type="UAH">
<currency>UAH</currency>
<value>33758.7525</value>
</newprice>
</price>
<producer>
<Company>Microsoft</Company>
<CompType>Corp</CompType>
</producer>
</portable_computers>
<portable_computers category="Tablets" id="10">
<img_main>https://images-na.ssl-images-amazon.com/images/I/51RkayoGnSL._SL1000_.jpg</img_main>
<name>*NEW*'Microsoft Surface Go 2 in 1 PC Tablet Education Bundle 10" '
'Touchscreen, 4GB RAM, 64GB Storage, Win 10 Pro, USB Type C, '
'Keyboard, Mouse and Digital Pen Included - Cobalt Blue (Renewed)'</name>
<link>https://www.amazon.com/Microsoft-Education-Touchscreen-Keyboard-Included/dp/B07W995R5N</link>
<price>
<newprice type="USD">
<currency>USD</currency>
<value>519.99</value>
<value_tf>498.99</value_tf>
</newprice>
<newprice type="UAH">
<currency>UAH</currency>
<value>12869.7525</value>
</newprice>
</price>
<producer>
<Company>Amazon</Company>
<CompType>Ltd</CompType>
</producer>
</portable_computers>
</product>
Помогите мне решить эту проблему:
- получитеобщее количество описанных элементов;
- получить информацию по одному атрибуту для всех описанных сложных элементов;
- получить информацию по одному атрибуту для одного сложного элемента;
- вывести номер элемента, имя которого состоит из более чем одного слова;
- вывести только первый параметр одного комплексного элемента;
- вывести только второй параметр одного комплексного элемента;
- выводит только третий параметр одного сложного элемента;
- Отображает имя элемента, если его цена находится в заданном диапазоне и содержит заданную последовательность букв в его имени;
- выводить каждый пятый элемент;
- выводить номер элемента и его цену для каждого второго сложного элемента в документе.
Вот то, что у меня есть до сих пор:
from xml.etree import ElementTree
from lxml import etree
doc = etree.parse("amazon_mod.xml")
#get the total number of elements described
print("1------------------------------")
count = doc.xpath("count(//portable_computers)")
print(count)
#get the information according to the one attribute for all described complex elements;
print("2------------------------------")
root = ElementTree.parse("amazon_mod.xml").getroot()
for name in root.findall("product/portable_computers/name"):
ElementTree.dump(name)
#Shows information of an element by searching its id, which equals "5"
print("3------------------------------")
for id in root.findall("product/portable_computers[@id='5']"):
ElementTree.dump(id)