2

dtの「投資を求めた」テキストを省略して、ddで「£70,004」テキストを抽出するにはどうすればよいですか。

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

1 に答える 1

4

as class attrib 値を持つddタグがないため、以下のように最後の 4 行を変更することをお勧めします。必要ない場合はinvestment_sought、最初の stmt を削除してください。print

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
>>> 
于 2015-12-23T11:12:13.407 に答える