简体   繁体   中英

Check if string in list present in HTML using BeautifulSoup

I'm using the following code to find text in my parsed HTML:

searched_word = "News"
results = parsedHTML.body.find_all(string=re.compile('.*{0}.*'.format(searched_word)), recursive=True)
if results:
    doStuff()

This works, but I'd like to use a list instead, eg:

searched_words = ["News", "Team"]

And if my parsed HTML has any of these string elements in its contents, should return True and what element was found in the HTML. I don't know how to accomplish this.

This might help.

searched_words = ["News", "Team"]
pattern = re.compile("|".join(searched_words))
results = parsedHTML.body.find_all(string=pattern, recursive=True)
if results:
    doStuff()

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