Select 标签在美丽的汤中有一些指定的文字
Select tags with some specified text in beautiful soup
在某些 html 页面上,我有一堆看起来像这样的标签:
<a class="country" href="www.google.com" title="Germany">09:18, 9 July 2021</a>
在 BeautifulSoup 中,我只需要 select 那些年份为 2019 年的德国标签(例如,示例标签不适合此处,因为它有 2021 年)。 '
最好的方法是什么?我只是从头开始学习 BS,到目前为止我只能这样做:
germany = germany_soup.find_all(attrs={"title": "Germany"})
然后检查 germany
中的每个标签的 text
属性是否包含 2019
.
我的问题:这是解决该问题的传统方法吗?有没有办法以某种方式在 find_all 中指定 '2019'
避免 'manual' 检查每个 tag.text 循环中是否有“2019”?
您可以使用 re
模块在所有标签中查找特定文本以提取合适的输出
html="""<a class="country" href="www.google.com" title="Germany">09:18, 9 July 2021</a>
<a class="country" href="www.google.com" title="Germany">09:18, 9 July 2019</a>
<a class="country" href="www.google.com" title="Germany">07:11, 9 July 2019</a>
<a class="country" href="www.google.com" title="Germany">09:18, 9 July 2010</a>
"""
import re
soup=BeautifulSoup(html,"html.parser")
soup.find_all("a",attrs={"title": "Germany"},text=re.compile("2019"))
输出:
[<a class="country" href="www.google.com" title="Germany">09:18, 9 July 2019</a>,
<a class="country" href="www.google.com" title="Germany">07:11, 9 July 2019</a>]
在某些 html 页面上,我有一堆看起来像这样的标签:
<a class="country" href="www.google.com" title="Germany">09:18, 9 July 2021</a>
在 BeautifulSoup 中,我只需要 select 那些年份为 2019 年的德国标签(例如,示例标签不适合此处,因为它有 2021 年)。 '
最好的方法是什么?我只是从头开始学习 BS,到目前为止我只能这样做:
germany = germany_soup.find_all(attrs={"title": "Germany"})
然后检查 germany
中的每个标签的 text
属性是否包含 2019
.
我的问题:这是解决该问题的传统方法吗?有没有办法以某种方式在 find_all 中指定 '2019'
避免 'manual' 检查每个 tag.text 循环中是否有“2019”?
您可以使用 re
模块在所有标签中查找特定文本以提取合适的输出
html="""<a class="country" href="www.google.com" title="Germany">09:18, 9 July 2021</a>
<a class="country" href="www.google.com" title="Germany">09:18, 9 July 2019</a>
<a class="country" href="www.google.com" title="Germany">07:11, 9 July 2019</a>
<a class="country" href="www.google.com" title="Germany">09:18, 9 July 2010</a>
"""
import re
soup=BeautifulSoup(html,"html.parser")
soup.find_all("a",attrs={"title": "Germany"},text=re.compile("2019"))
输出:
[<a class="country" href="www.google.com" title="Germany">09:18, 9 July 2019</a>,
<a class="country" href="www.google.com" title="Germany">07:11, 9 July 2019</a>]