2011-02-09 100 views
3

如何使用BeautifulSoup插入屬性?如何使用BeautifulSoup插入屬性?

例如,插入border =「1」作爲標籤屬性。

編輯:

我已經回答了我自己的問題(一類特殊的表,甚至):

inTopic = urllib2.urlopen(「文件:/// C:/檢驗/試驗html的「) 含量= BeautifulSoup(inTopic)

TLIST = content.findAll( '表', 」myTableClass「) 爲TBL中TLIST: TBL [ '邊界'] = 」1「 打印TBL。 attrs

+0

發佈您的解決方案作爲答案,然後接受它...? – smci 2011-07-04 09:45:12

回答

4

如何:

inTopic = urllib2.urlopen('http://stackoverflow.com/questions/4951331/how-do-i-insert-an-attribute-using-beautifulsoup') 
content = BeautifulSoup.BeautifulSoup(inTopic) 
tlist = content.findAll('table') 
for tbl in tlist: 
    tbl.attrs.append(('border', 1)) 

不要忘了試一試lxml.html,它很快速,解析得很好。