Python BeautifulSoup:通配符属性/id 搜索 [英] Python BeautifulSoup: wildcard attribute/id search
本文介绍了Python BeautifulSoup:通配符属性/id 搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个:
dates = soup.findAll("div", {"id" : "date"})
但是,我需要 id 作为通配符搜索,因为 id
可以是 date_1
、date_2
等
However, I need id to be a wildcard search since the id
can be date_1
, date_2
etc.
推荐答案
您可以提供一个可调用对象作为过滤器:
You can provide a callable as a filter:
dates = soup.findAll("div", {"id" : lambda L: L and L.startswith('date')})
或者正如@DSM 指出的那样
Or as @DSM points out
dates = soup.findAll("div", {"id" : re.compile('date.*')})
因为 BeautifulSoup 会识别一个 RegExp 对象并调用它的 .match()
方法.
as BeautifulSoup will recognise a RegExp object and call its .match()
method.
这篇关于Python BeautifulSoup:通配符属性/id 搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文