亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用BeautifulSoup Python解析表

使用BeautifulSoup Python解析表

肥皂起泡泡 2021-03-29 16:43:33
如果要讀取遵循以下格式的表中的條目:<table cellspacing="0" cellpadding="4">stuff</table>我將其用作當前方法:pg = urllib2.urlopen(req).read()page = BeautifulSoup(pg)table = page.find('table', cellpadding = 4, cellspacing = 0)我table無法正確讀取標簽,什么是最好的方法?
查看完整描述

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)。


查看完整回答
反對 回復 2021-04-06
  • 1 回答
  • 0 關注
  • 266 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號