1

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?

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

3 Comments

hey man i have a follow up a question, would appreciate it if you could help on this one too. i edited the question
@Hamza Don't edit the question with new followup question - it creates lots of confusion. Revert it back, post new question here on SO. I'll look at it.
oh Ok, will do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.