Find a specific tag with BeautifulSoup

Deadly 提交于 2019-12-30 02:38:07

问题


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?


回答1:


The following should work

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

There are couple of ways to search for tags.

  • http://www.crummy.com/software/BeautifulSoup/documentation.html

For more text to understand and use it

  • http://lxml.de/elementsoup.html



回答2:


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



来源:https://stackoverflow.com/questions/3945750/find-a-specific-tag-with-beautifulsoup

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!