简体   繁体   中英

Reading xml from an URL python

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM