XXX
XXX

Reputation: 63

'NoneType' object has no attribute 'text'

How should I extract "£70,004" text in dd, omitting "Investment sought" text which in dt.

from bs4 import BeautifulSoup
import urllib2

url="https://www.seedrs.com/tanorganic"
page = urllib2.urlopen(url)
soup = BeautifulSoup(page.read(), "html.parser")

target = soup.find("dl", class_="investment_sought").text

print target

figure = soup.find("dd", class_="investment_sought").text

print figure

result :

Investment

sought:

£70,004

Traceback (most recent call last):
  File "testing.py", line 12, in <module>
    figure = soup.find("dd", class_="investment_sought").text
AttributeError: 'NoneType' object has no attribute 'text'

Upvotes: 2

Views: 62300

Answers (2)

Swapnil Jadhav
Swapnil Jadhav

Reputation: 1

try:

this will return text

figure = soup.find("dd", class_="investment_sought").text except AttributeError:

This will return None

figure = soup.find("dd", class_="investment_sought")

Upvotes: -2

Avinash Raj
Avinash Raj

Reputation: 174874

I suggest you to change the last 4 lines like below since there isn't a dd tag with investment_sought as class attrib value. Remove the first print stmt if you don't want..

target = soup.find("dl", class_="investment_sought")
print target.text
figure = target.find("dd").text
print figure

Example:

>>> from bs4 import BeautifulSoup
>>> import urllib2
>>> url="https://www.seedrs.com/tanorganic"
>>> page = urllib2.urlopen(url)
>>> soup = BeautifulSoup(page.read(), "html.parser")
>>> target = soup.find("dl", class_="investment_sought")
>>> print target.text


Investment

sought:

£70,004

>>> figure = target.find("dd").text
>>> print figure
£70,004
>>> 

Upvotes: 4

Related Questions