我有这个:
dates = soup.findAll("div", {"id" : "date"})
但是,我需要 id 作为通配符搜索,因为 id 可以是 date_1、date_2 等。
最佳答案
您可以提供一个可调用对象作为过滤器:
dates = soup.findAll("div", {"id" : lambda L: L and L.startswith('date')})
或者正如@DSM 指出的那样
dates = soup.findAll("div", {"id" : re.compile('date.*')})
因为 BeautifulSoup 将识别 RegExp 对象并调用其 .match() 方法。
关于Python BeautifulSoup : wildcard attribute/id search,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14257717/