Skip to content
Advertisement

How to access a specific p tag while using BeautifulSoup

Hello everyone I’m having trouble while using BeautifulSoup , indeed i don’t succeed to access the information that I want, here is my code :

JavaScript

The output of this code is that :

JavaScript

and what I want is the second ‘p’ with the information : ’10. March 2021′ however I don’t know how to access this information, I tried : print(results_date.find(‘p’)) instead of print(results_date) but it gives me the first ‘p’ meaning it gives me: ‘p class=”bodytext”>2021’ and it’s stil not what I want, if anyone can help me it will be very nice.

Advertisement

Answer

You can use the :nth-of-type(n) CSS Selector.

To use a CSS Selector, use the .select() method instead of .find_all().


In your example, find the id="c51", and then the second p tag.

JavaScript

Output:

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