我有一个HTML文件,里面有以下代码:
<...some tags...>
<textarea id="123" attributeX="4" attributeY="5" style="width:159px; height:50px; other styles;">
<textarea id="456" attributeX="4" attributeY="5" style="width:135px; height:50px; other styles;">
<textarea id="789" attributeX="4" attributeY="5" style="width:177px; height:50px; other styles;">
<...some other tags...>
我想通过使用Python 2.0将所有文本区域的宽度更改为200px.
from bs4 import BeautifulSoup
from cssutils import parseStyle
html = '<td style="font-size: .8em; font-family: monospace; background-color: rgb(244, 244, 244);"></td>'
soup = BeautifulSoup(html, 'html.parser')
style = parseStyle(soup.td['style'])
style['background-color'] = 'red'
soup.td['style'] = style.cssText
print(soup.td)
不幸的是,这只会改变one标签的样式.
我try 了以下代码:
from bs4 import BeautifulSoup
from cssutils import parseStyle
soup = BeautifulSoup(sHTML,'html.parser')
for txt in soupfindAll('textarea'):
style = parseStyle(text.textarea['style'])
style['width'] = '200px'
txt.textarea['style'] = style.cssText
这会在"style=…"行生成一个"非类型对象不可下标"错误
有人知道我如何执行所需的格式吗?
谢谢