OK 400
OK 400

Reputation: 831

How can I scrape this page?

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

Answers (3)

Sebastien D
Sebastien D

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

AI Humanizer
AI Humanizer

Reputation: 432

try this one:

links = soup.findAll('a', attrs={'class':'class-2'}).text 
for link in links:     
    title = link.get('title')

Upvotes: 1

Equinox
Equinox

Reputation: 6758

from the docs. You can write tag[attr_name] to get single attribute and tag.attrs lo get a dictionary of all attributes with values.

soup.find('a', attrs={'class':'class-2'})['title']

Upvotes: 1

Related Questions