1

Hey I am scraping an HTML site and getting issues when wanting to access some objects.

products = BeautifulSoup(response.text, "html.parser").findAll("div", {"data-rubrik":"Cars"})

^This results in an array of 100+ objects. Each of these objects contains an aria-label looking like that:

<div class="art artikelid an" data-alter="7" data-hauptrubrik="Cars" data-k="1666377008.07714">
<a aria-label="Ford Mustang"></a>
</div>

I want to loop through all of the objects and print the car name (aria-label) for each object.

This would be the right way to archive it in JS:

for (let i = 0; i < products.length; i++) {
    let cars = products[i].split('aria-label="')[1].split('"')[0]; 
}

But i cant get it to work in python this is my attempt:

for cars in products:
                print(cars.split('aria-label="')[1].split('"')[0])

I hope someone can help me out. Have a great evening :)

1 Answer 1

1

Try:

for cars in products:
    a = cars.find("a")
    if a:
        print(a.get("aria-label"))
Sign up to request clarification or add additional context in comments.

2 Comments

thank you this worked but how if an object has multiuple "a" tags how would i get the 3rd or 4th one ?
@mxxim Then try a = cars.find_all("a")[3] or other index.

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.