如何在dd中提取“£70,004”文本,省略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
结果:
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'
答案 0 :(得分:4)
我建议您更改最后4行,如下所示,因为没有dd
标记investment_sought
作为类属性值。如果您不想要删除第一个print
stmt ..
target = soup.find("dl", class_="investment_sought")
print target.text
figure = target.find("dd").text
print figure
示例:的
>>> 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
>>>