Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find a specific tag with BeautifulSoup

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?

like image 212
Jane Avatar asked Oct 15 '10 20:10

Jane


2 Answers

The following should work

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

There are couple of ways to search for tags.

  • https://www.crummy.com/software/BeautifulSoup/bs4/doc/#searching-the-tree

For more text to understand and use it

  • http://lxml.de/elementsoup.html
like image 66
pyfunc Avatar answered Nov 16 '22 20:11

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;"})

like image 35
0xMH Avatar answered Nov 16 '22 20:11

0xMH