Print all links that may contain any of the words from a list

Home / Uncategorized / Print all links that may contain any of the words from a list

Question:
How to print all links from a webpage that contains a word from a list of words. I got the list of links that contains the word ‘contact’ using Beautifulsoup and the code goes like below.
soup = BeautifulSoup(html_page, "html.parser")
for link in soup.findAll(‘a’):links = link.get(‘href’)
if ‘contact’ in links:print links
But how can I print links with the following condition
keywords = ("contact", "reach", "feedback", "address")
if links contain any words from keywords: ## this is not actual code
print links;


Answer:
This is likely a duplicate, though I’m on mobile so I’m having difficulty finding another question that answers yours.

In brief, what you probably want is a list comprehension and any
if any( [word in links for word in keywords] )
any returns True if there’s at least one True value in the list (which, fun tidbit, is basically functionally equivalent to sum because of how Python casts Booleans to ints). And the list comprehension generates a Boolean list for all evaluations of the keyword being in the link.
Read more

Leave a Reply

Your email address will not be published. Required fields are marked *