How to find tags with only certain attributes - BeautifulSoup

后端 未结 6 789
被撕碎了的回忆
被撕碎了的回忆 2020-11-28 05:23

How would I, using BeautifulSoup, search for tags containing ONLY the attributes I search for?

For example, I want to find all

6条回答
  •  时光取名叫无心
    2020-11-28 05:49

    You can use lambda functions in findAll as explained in documentation. So that in your case to search for td tag with only valign = "top" use following:

    td_tag_list = soup.findAll(
                    lambda tag:tag.name == "td" and
                    len(tag.attrs) == 1 and
                    tag["valign"] == "top")
    

提交回复
热议问题