1 回答

TA貢獻1806條經驗 獲得超5個贊
我已經在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)。
添加回答
舉報