简体   繁体   中英

Beautiful Soup filtering for more than one keyword

CODE

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'])

Is it possible to filter for more than one, like this?

text='CompanyA' OR text='CompanyB' OR text='CompanyC'

这将为您提供所有具有text属性并与您的文本列表匹配的元素。

soup.findAll('a', href=True, text=lambda value: value and value in ["CompanyA", "CompanyB", "CompanyC"])

Use regular expression.

import re
for link in soup.findAll("a", href=True,text=re.compile("CompanyA|CompanyB|CompanyC")):
        print (link['href'])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM