Python Web Scraping using BeautifulSoup AttributeError: 'NoneType' object has no attribute 'text'

Viewed 585

I am not sure why I am getting an error message AttributeError: 'NoneType' object has no attribute 'text' while running a code in Python 2 on WebScraping using BeautifulSoup?

Error message :

AttributeError Traceback (most recent call last)

in ()

----> 1 name = name_box.text.strip()

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

Following is my code:

from bs4 import BeautifulSoup

import requests

import csv

source = requests.get('http://coreyms.com').text

soup = BeautifulSoup(source, 'lxml')

name_box = soup.find('h1', attrs = {'class': 'name'})

name = name_box.text.strip()

Could you please help. Thanks!

1 Answers

You are getting that response b'coz there is no h1 tag eith the class "name" in the page. The only h1 tag i was able to find had a class of "site-title".

from bs4 import BeautifulSoup
import requests
import csv
source = requests.get('http://coreyms.com').text
soup = BeautifulSoup(source, 'lxml')
name_box = soup.find('h1', attrs = {'class': 'site-title'})
name = name_box.text.strip()
print(name)

Output

CoreyMS
Related