Beautifullsoup: How to grab the contents of an <a> tag thats within a <h1>?

Viewed 41

I have been trying to extract the name from a twitter profile, the only problem I'm having is that beautifulsoup grabs the entire element. I have tried the {"class":} to specify the element but whenever I do this it results in getting

AttributeError: 'NoneType' object has no attribute 'text' error.

My code:

url = "https://twitter.com/barackobama"
html_doc = urllib.request.urlopen(url)
soup = BeautifulSoup(html_doc, 'lxml')

name = soup.find('h1').text
print(name)
1 Answers

If you want to get the text from child link of header instead of complete header text, try

url = "https://twitter.com/barackobama"
html_doc = urllib.request.urlopen(url)
soup = BeautifulSoup(html_doc, 'lxml')

name = soup.find('h1').a.text
print(name)
# 'Barack Obama'
Related