Issue
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'
Solution
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
>>>
Answered By - Avinash Raj
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.