Reputation: 2296
I would like to select the second element by specifying the fact that it contains "title" element in it (I don't want to just select the second element in the list)
sample = """<h5 class="card__coins">
<a class="link-detail" href="/en/coin/smartcash">SmartCash (SMART)</a>
</h5>
<a class="link-detail" href="/en/event/smartrewards-812" title="SmartRewards">"""
How could I do it? My code (does not work):
from bs4 import BeautifulSoup
soup = BeautifulSoup(sample.content, "html.parser")
second = soup.find("a", {"title"})
Upvotes: 0
Views: 753
Reputation: 11
Another way to do this is by using CSS selector
soup.select_one('a[title]')
this selects the first a
element having the title
attribute.
Upvotes: 0
Reputation: 335
for I in soup.find_all('a', title=True):
print(I)
After looping through all a
tags we are checking if it contains title attribute and it will only print it if it contains title attribute.
Upvotes: 2