python - beautifulsoup - 如何查找以特定属性开头的标签?

标签 python html-parsing beautifulsoup

例如,我有:

<a class="banana" href="http://example.com">link1</a>
<a href="http://example2.com" class="banana"><img ... /></a>
<a class="banana">link2</a>
<a href="http://google.com">link3</a>

如何获得:

['<a href="http://example2.com" class="banana"><img ... /></a>','<a href="http://google.com">link3</a>']

最佳答案

您可以使用 css 选择器 a[href] 获取带有 href 属性的 a 标签:

h = '''
<a class="banana" href="http://example.com">link1</a>
<a href="http://example2.com" class="banana"><img ... /></a>
<a class="banana">link2</a>
<a href="http://google.com">link3</a>
'''

from bs4 import BeautifulSoup
soup = BeautifulSoup(h)
print(soup.select('a[href]'))

输出:

[<a class="banana" href="http://example.com">link1</a>,
 <a class="banana" href="http://example2.com"><img ...=""/></a>,
 <a href="http://google.com">link3</a>]

关于python - beautifulsoup - 如何查找以特定属性开头的标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22124257/

相关文章:

用于清理 html 的 C# 库

python - 网络抓取 - 基本库存

Python - 如何在字典中递归添加文件夹的内容

python - 即使文件存在也没有文件错误

php - 使用 PHP 简单 HTML DOM 解析器的奇怪错误

java - 解析复杂的 li 标签

python - 正确的Url生成 flask

python - 在 Pandas 中解析 JSON

python - 如何使用 Python 从网站上抓取文本

python - 如何从 ubuntu 服务器以编程方式登录 Yahoo