Home > Blockchain >  How do I scrap the text of anchor tag using Beautifulsoup?
How do I scrap the text of anchor tag using Beautifulsoup?

Time:03-07

   <span >
<a data-tn-element="companyName"  target="_blank" href="/cmp/Lush-Cosmetics" rel="noopener">LUSH Cosmetics</a>
</span>

I did this - item.find('span', class_ ='companyName').find('a').text

But it gives me the error - item.find('span', class_ ='companyName').find('a').text AttributeError: 'NoneType' object has no attribute 'text'

CodePudding user response:

This should do the trick

item.find('span',{'class':'companyName'}).find('a').text

Working code

from bs4 import BeautifulSoup

content = '''
<div >
<span >
<a data-tn-element="companyName"  target="_blank" href="/cmp/Lush-Cosmetics" rel="noopener">LUSH Cosmetics</a>
</span></div>
'''

soup = BeautifulSoup(content,features="lxml")
anchorText = soup.find('span',{'class':'companyName'}).find('a').text
print(anchorText)
  • Related