Find div id with variations - Web Scraping - Beautifulsoup

Asked

Viewed 103 times

0

Assuming I have the following div ids

<div id='post_message_12932087>
    <p>random text</p>
</div>

<div id='post_message_21390123>
    <p>random text2</p>
</div>

<div id='post_message_23190495>
    <p>random text3</p>
</div>

How would I get the data (using find_all()) from each of these div ids, the default of each div id being the following "post_message_numero"?

pagina.find_all('div', {'id', 'post_message_xxxx'})

1 answer

3


Use the CSS equivalent selector for attributes that checks whether the ID starts with how post_message_: [atributo^=valor]

The dial should be something like div[id^=post_message_] along with the method select() of Beautiful Soup or select_one() if you want to catch only the first occurrence, example:

pagina.select('div[id^=post_message_]')

To catch only the first:

pagina.select_one('div[id^=post_message_]')
  • Perfect! Thank you very much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.