簡體   English   中英

美麗的湯過濾多個關鍵字

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM