python - 如何使用 BeautifulSoup 插入属性?

标签 python beautifulsoup

如何使用 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/

相关文章:

python - 尝试使用 Beautiful Soup 或 ElementTree 从链接的迭代列表中抓取信息

python - 如何使用 python beautifulsoup 向 html 文件插入新行

python - VS Code 中 anaconda 环境的“无法导入”错误

python - Python列表中的多个 'for'循环

python - 在pyqtgraph中鼠标交互后将缩放恢复到默认级别

python - 从解释器和命令行使用 timeit 的时间差异

python - TypeError : 'str' object is not callable with input()

Python:Selenium 和 PhantomJS

python - 根据各种参数选择div

python - 从 HTML 页面获取数据