如何使用 BeautifulSoup 插入属性?
例如,插入border="1"
作为<table>
标记属性。
编辑:
我已经回答了我自己的问题(对于特定类别的表,甚至):
inTopic = urllib2.urlopen("file:///C:/test/test.html")
content = BeautifulSoup(inTopic)
tlist = content.findAll('table', "myTableClass")
for tbl in tlist:
tbl['border'] = "1"
print tbl.attrs
最佳答案
怎么样:
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
,它速度快且解析良好。
关于python - 如何使用 BeautifulSoup 插入属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4951331/