なぜ「結果」に属性「findall」がないのですか?
-
13-09-2019 - |
質問
そのため、私はPythonをゆっくりと学んでおり、オンラインゲームのハイスコアページからデータを描画する単純な関数を作成しようとしています。これは、私が1つの関数に書き直した他の誰かのコードです(これが問題になる可能性があります)が、このエラーが発生しています。これがコードです:
>>> from urllib2 import urlopen
>>> from BeautifulSoup import BeautifulSoup
>>> def create(el):
source = urlopen(el).read()
soup = BeautifulSoup(source)
get_table = soup.find('table', {'id':'mini_player'})
get_rows = get_table.findAll('tr')
text = ''.join(get_rows.findAll(text=True))
data = text.strip()
return data
>>> create('http://hiscore.runescape.com/hiscorepersonal.ws?user1=bigdrizzle13')
Traceback (most recent call last):
File "<pyshell#18>", line 1, in <module>
create('http://hiscore.runescape.com/hiscorepersonal.ws?user1=bigdrizzle13')
File "<pyshell#17>", line 6, in create
text = ''.join(get_rows.findAll(text=True))
AttributeError: 'ResultSet' object has no attribute 'findAll'
前もって感謝します。
解決
わお。 Triptychが提供しました すごい 答え 関連する質問に。
見える、 BeautifulSoupのソースコードから, 、 それ ResultSet
サブクラス list
.
あなたの例では、 get_rows
BSのインスタンスです ResultSet
クラス、
そしてBS以来 ResultSet
サブクラス list
, 、それは意味します get_rowsはリストです.
get_rows
, 、のインスタンスとして ResultSet
, 、します いいえ 持っている findAll
実装された方法。したがって、エラー。
Triptychが違ったやり方をしたことは何ですか 反復 そのリストに。
Triptychの方法は、のアイテムが機能するためです get_rows
リストは、BSのタグクラスのインスタンスです。を持っています findAll
方法。
だから、あなたのコードを修正するために、あなたの最後の3行を交換することができます create
次のような方法:
for row in get_rows:
text = ''.join(row.findAll(text=True))
data = text.strip()
print data
レナード・リチャードソンへのメモ:それをBSと呼ぶことであなたの仕事の質を軽meanするつもりはありません;-)
所属していません StackOverflow