I'm trying to capture all the relevant links from a web page with beautiful soup. All the links I need have both the class="btn btn-gray"
and also the text <a...>More Info<>
What's the best way to extract just these links?
How about this?
soup = BeautifulSoup(html, 'lxml')
all_links = []
links = soup.find_all('a', {'class': ['btn', 'btn-gray']})
for link in links:
if 'More Info' in link.text:
all_links.append(link['href']) # Save href only, for example.
or as a clean list comprehension:
links = soup.find_all('a', {'class': ['btn', 'btn-gray']})
results = [link['href'] for link in links if 'More Info' in link.text]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With