1

次の形式のテーブルのエントリを読みたい場合:

<table cellspacing="0" cellpadding="4">

stuff

</table>

私はこれを現在の方法として使用しています:

pg = urllib2.urlopen(req).read()
page = BeautifulSoup(pg)
table = page.find('table', cellpadding = 4, cellspacing = 0)

タグを正しく読み取れtableません。これを行う最善の方法は何ですか?

4

1 に答える 1

1

BeautifulSoup バージョン 3 と 4 の両方でこれをテストしました。コードは BS4 で動作するため、バージョン 3 を使用している必要があります。

>>> from bs4 import BeautifulSoup as BS4 # Version 4
>>> from BeautifulSoup import BeautifulSoup as BS3 # Version 3
>>> bs3soup = BS3("""<table cellspacing="0" cellpadding="4">
... 
... stuff
... 
... </table>""")
>>> bs4soup = BS4("""<table cellspacing="0" cellpadding="4">
... 
... stuff
... 
... </table>""")
>>> bs3soup.find('table', cellpadding = 4, cellspacing = 0) # None
>>> bs4soup.find('table', cellpadding = 4, cellspacing = 0)
<table cellpadding="4" cellspacing="0">

stuff

</table>

したがって、BS3 を引き続き使用する場合は、次の手順で修正します。

>>> soup.find('table', cellpaddin="4", cellspacing="0") # Notice how the integers are now strings, like in the HTML.

ただし、バージョン 4 ( ) を使用する必要がありますfrom bs4 import BeautifulSoup

于 2013-06-27T01:21:25.000 に答える