'NoneType' 对象没有属性 'text'

'NoneType' object has no attribute 'text'

我应该如何提取 dd 中的“70,004 英镑”文本,省略 dt 中的 "Investment sought" 文本。

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'

我建议您像下面这样更改最后 4 行,因为没有 dd 标记将 investment_sought 作为 class 属性值。如果您不想,请删除第一个 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
>>>