Python 使用 BeautifulSoup 查找特定标签

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3945750/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 13:28:37  来源:igfitidea点击:

Find a specific tag with BeautifulSoup

pythonbeautifulsoup

提问by Jane

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?

我可以使用 BS 轻松遍历通用标签,但我不知道如何找到特定标签。例如,我怎样才能找到所有出现的<div style="width=300px;">?这可以用 BS 吗?

采纳答案by pyfunc

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

更多文字了解和使用

回答by 0xMH

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

有了 bs4,事情发生了一些变化。所以代码应该是这样的

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

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