Skip to content
Advertisement

How to find a tag within the same parent that has the child I want?

Scraping a website that has multiple products on the same page, some that I don’t want to know the prices of. So I wanted to first see the product category to then get the price listed.

The website code looks like this:

JavaScript

I already know how to get to the category part with my own code, but I’m completely stuck on the other part.

JavaScript

How can I get to the specific price tag within this parent <section> tag?

Advertisement

Answer

You are close to your goal but be aware that products.text will give you the whole section text, better use products.span.text to get the category text only.

To get the price info, simply find the span with class="price" and check if it is available or not to avoid errors:

JavaScript
Example
JavaScript
Output
JavaScript

As alternative an approach that is more lean, creates a structured output that is easy to process and deals with a list of permitted categories:

JavaScript
Output
JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement