1

I am trying to check if an element has an other element inside. the url element sometime contain loc tag alone and sometimes loc and image tag, i want to get the value of the loc tag when there is an image tag as well, i tried something like this.

url = "https://www.aeroprecisionusa.com/media/sitemap_en.xml"
response = requests.get(url)
root = ET.fromstring(response.content)
links = []
for elm in root.findall(".//{http://www.sitemaps.org/schemas/sitemap/0.9}url"):
    if elm.find('.//{http://www.sitemaps.org/schemas/sitemap/0.9}image) is not None:
        link = elm.find('./{http://www.sitemaps.org/schemas/sitemap/0.9}loc').text
        links.append(link)
        return links

but still it returns loc tag of all url parent element.

1 Answer 1

1

This script will print <loc> tags and <image> urls tags:

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)
links = []
for elm in root.findall(".//{http://www.sitemaps.org/schemas/sitemap/0.9}url"):
    loc = elm.find(".//{http://www.sitemaps.org/schemas/sitemap/0.9}loc")
    img = elm.find(".//{http://www.google.com/schemas/sitemap-image/1.1}image")
    if not loc is None and not img is None:
        img_loc = img.find(
            ".//{http://www.google.com/schemas/sitemap-image/1.1}loc"
        )
        print(loc.text)
        print(img_loc.text)
        print("-" * 80)

Prints:

...

--------------------------------------------------------------------------------
https://www.aeroprecisionusa.com/magpul-moe-grip-sl-s-stock-midnight-marshland-furniture-set
http://d2df4e9l5rljaz.cloudfront.net/media/catalog/product/cache/61578878e6753b4ec73e244e03a0515d/a/p/aprh101361c-magpul-moe-grip-sl-s-stock-midnight-marshland-furniture-set-3.jpg
--------------------------------------------------------------------------------
https://www.aeroprecisionusa.com/battle-rope-2pt0-357-38-cal-9mm-pistol
http://d2df4e9l5rljaz.cloudfront.net/media/catalog/product/cache/61578878e6753b4ec73e244e03a0515d/a/p/aprh101753-battle-rope-2pt0.jpg
--------------------------------------------------------------------------------
https://www.aeroprecisionusa.com/battle-2pt0-rope-22-223-cal-pistol-rifle
http://d2df4e9l5rljaz.cloudfront.net/media/catalog/product/cache/61578878e6753b4ec73e244e03a0515d/a/p/aprh101754-battle-rope-2pt0.jpg
--------------------------------------------------------------------------------
Sign up to request clarification or add additional context in comments.

Comments

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.