BeautifulSoup 查找包含特定单词的链接 [英] BeautifulSoup to find a link that contains a specific word
本文介绍了BeautifulSoup 查找包含特定单词的链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个链接:
<a href="/location/santa-clara/3fce50c4f3f9793d2f503fc145585090">Santa Clara, California</a>
如何使用 BeautifulSoup 专门查找包含位置"一词的此链接?
How can I use BeautifulSoup to find specifically this link that includes the word location "location"?
推荐答案
你可以用一个简单的 "包含" CSS 选择器:
You can do it with a simple "contains" CSS selector:
soup.select("a[href*=location]")
或者,如果只需要匹配一个链接,使用select_one()
:
Or, if only one link needs to be matched, use select_one()
:
soup.select_one("a[href*=location]")
当然,还有许多其他方法 - 例如,您可以使用 find_all()
提供了 href
参数,它可以有一个 正则表达式 值或 function:
And, of course, there are many other ways - for instance, you can use find_all()
providing the href
argument which can have a regular expression value or a function:
import re
soup.find_all("a", href=re.compile("location"))
soup.find_all("a", href=lambda href: href and "location" in href)
这篇关于BeautifulSoup 查找包含特定单词的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文