import urllib2 from bs4 import BeautifulSoup html = urllib2.urlopen("hogehoge") soup = BeautifulSoup(html, "html.parser") tag =soup.find("td", class_="hogehoge").string print(tag.encode("utf-8"))
[vagrant@localhost python]$ python app.py
22,697.88
米ドル、ニューヨークダウ、上海も行きたい。
find_allで書くと、tracebackが出てくる。何故だ。
tag =soup.find_all("td", class_="hogehoge").string print(len(tag))
[vagrant@localhost python]$ python app.py
Traceback (most recent call last):
File “app.py”, line 8, in
tag =soup.find_all(“td”, class_=”header_shisuu_atai1″).string
File “/home/linuxbrew/.linuxbrew/Cellar/python@2/2.7.14_4/lib/python2.7/site-packages/bs4/element.py”, line 1807, in __getattr__
“ResultSet object has no attribute ‘%s’. You’re probably treating a list of items like a single item. Did you call find_all() when you meant to call find()?” % key
AttributeError: ResultSet object has no attribute ‘string’. You’re probably treating a list of items like a single item. Did you call find_all() when you meant to call find()?
soup.find_all(“a”)だと行けるので、find_all()の中がまずそうだ。
findとfind_allだと、書き方が違うのか。。。リストは行けるんだが。。
[
,
,
,
]