I am reading an XML file from a URL, but when I try to locate elements in it, it doesn’t work and finds nothing.
url = "https://www.aeroprecisionusa.com/media/sitemap_en.xml" response = requests.get(url) root = ET.fromstring(response.content) for elm in root.findall('.//loc'): print(elm)
here is the XML I am working with: https://www.aeroprecisionusa.com/media/sitemap_en.xml how to parse this XML from this URL and locate all loc tags or elements?
Answer
The <loc>
element is inside namespace http://www.sitemaps.org/schemas/sitemap/0.9
:
import requests import xml.etree.ElementTree as ET url = "https://www.aeroprecisionusa.com/media/sitemap_en.xml" response = requests.get(url) root = ET.fromstring(response.content) for elm in root.findall(".//{http://www.sitemaps.org/schemas/sitemap/0.9}loc"): print(elm.text)
Prints:
... https://www.aeroprecisionusa.com/magpul-moe-grip-sl-stock-midnight-marshland-furniture-set https://www.aeroprecisionusa.com/magpul-moe-grip-sl-s-stock-midnight-marshland-furniture-set https://www.aeroprecisionusa.com/magpul-moe-grip-prs-gen3-stock-midnight-marshland-furniture-set https://www.aeroprecisionusa.com/battle-rope-2pt0-357-38-cal-9mm-pistol https://www.aeroprecisionusa.com/battle-2pt0-rope-22-223-cal-pistol-rifle