Find a specific tag with BeautifulSoup

Question:

I can traverse generic tags easily with BS, but I don’t know how to find specific tags. For example, how can I find all occurances of <div style="width=300px;">? Is this possible with BS?

Asked By: Jane

||

Answers:

The following should work

soup = BeautifulSoup(htmlstring)
soup.findAll('div', style="width=300px;")

There are couple of ways to search for tags.

For more text to understand and use it

Answered By: pyfunc

with bs4 things have changed a little. so the code should look like this

soup = BeautifulSoup(htmlstring,'lxml')
soup.find_all('div', {'style':"width=300px;"})

Answered By: 0xMH
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.