Reputation: 831
I'm scraping a page, but I'm getting errors trying to scrape WANTED-DATA
<td class="class-1" data-reactid="41"><a class="class-2" data-reactid="42" data-symbol="MORE-DATA" href="/quote/HKxlkPH4-x" title="WANTED-DATA">text</a></td>
The closer thing I can extract is text
by doing:
getText.find('a', attrs={'class':'class-2'}).text
# output: 'text'
How can I scrape 'WANTED-DATA'
?
Upvotes: 0
Views: 68
Reputation: 4482
You could do it also like this:
html = """<td class="class-1" data-reactid="41"><a class="class-2" data-reactid="42" data-symbol="MORE-DATA" href="/quote/HKxlkPH4-x" title="WANTED-DATA">text</a></td>"""
soup = BeautifulSoup(html)
## adding title=True below prevent any error in case you have links without the 'title attribute'
titles = [x.get('title') for x in soup.find_all('a',title=True)]
print(titles)
Output:
['WANTED-DATA']
Upvotes: 0
Reputation: 432
try this one:
links = soup.findAll('a', attrs={'class':'class-2'}).text
for link in links:
title = link.get('title')
Upvotes: 1