[英]Beautiful Soup filtering for more than one keyword
碼
soup = BeautifulSoup(urllib.request.urlopen(link['href']).read(), 'lxml')
# Find CompanyA links
for link in soup.findAll('a', href=True, text='CompanyA'):
print (link['href'])
這樣可以過濾多個嗎?
text='CompanyA' OR text='CompanyB' OR text='CompanyC'
這將為您提供所有具有text屬性並與您的文本列表匹配的元素。
soup.findAll('a', href=True, text=lambda value: value and value in ["CompanyA", "CompanyB", "CompanyC"])
使用正則表達式。
import re
for link in soup.findAll("a", href=True,text=re.compile("CompanyA|CompanyB|CompanyC")):
print (link['href'])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.