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"?
推荐答案
您可以使用简单的:
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
参数,该参数可以具有正则表达式值或功能:
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屋!
查看全文