我有一个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=…"行生成一个"非类型对象不可下标"错误

有人知道我如何执行所需的格式吗?

谢谢

推荐答案

try 以下方法:

from bs4 import BeautifulSoup
from cssutils import parseStyle

with open('input.html') as f_html:
    soup = BeautifulSoup(f_html, 'html.parser')
    
for textarea in soup.find_all('textarea', style=True):
    style = parseStyle(textarea['style'])
    style['width'] = '200px'
    textarea['style'] = style.cssText.replace('\n', ' ')

with open('output.html', 'w', encoding='utf-8') as f_html:
    f_html.write(str(soup))

如果你的HTML是:

<html>
<body>
<textarea id="123" attributeX="4" attributeY="5" style="width:159px; height:50px;"></textarea>
<textarea id="456" attributeX="4" attributeY="5" style="width:135px; height:50px;"></textarea>
<textarea id="789" attributeX="4" attributeY="5" style="width:177px; height:50px;"></textarea>
<textarea id="789" attributeX="4" attributeY="5"></textarea>
</body>
</html>

结果将是:

<html>
<body>
<textarea attributex="4" attributey="5" id="123" style="width: 200px; height: 50px"></textarea>
<textarea attributex="4" attributey="5" id="456" style="width: 200px; height: 50px"></textarea>
<textarea attributex="4" attributey="5" id="789" style="width: 200px; height: 50px"></textarea>
<textarea attributex="4" attributey="5" id="789"></textarea></body>
</html>

这里的最后<textarea>名没有改变,因为没有风格.如有需要,可添加如下内容:

from bs4 import BeautifulSoup
from cssutils import parseStyle

with open('input.html') as f_html:
    soup = BeautifulSoup(f_html, 'html.parser')
    
for textarea in soup.find_all('textarea'):
    if 'style' in textarea.attrs:
        # Update existing style
        style = parseStyle(textarea['style'])
        style['width'] = '200px'
        textarea['style'] = style.cssText.replace('\n', ' ')
    else:
        # Add missing style
        textarea['style'] = 'width: 200px; height: 50px'

with open('output.html', 'w', encoding='utf-8') as f_html:
    f_html.write(str(soup))

Python相关问答推荐

PyQt5如何将pyuic 5生成的Python类添加到QStackedWidget中?

如何在Python中使用ijson解析SON期间检索文件位置?

Python无法在已导入的目录中看到新模块

查找下一个值=实际值加上使用极点的50%

在内部列表上滚动窗口

对Numpy函数进行载体化

即使在可见的情况下也不相互作用

运行总计基于多列pandas的分组和总和

从numpy数组和参数创建收件箱

2D空间中的反旋算法

在np数组上实现无重叠的二维滑动窗口

pandas:排序多级列

多处理队列在与Forking http.server一起使用时随机跳过项目

如何在Pyplot表中舍入值

将标签移动到matplotlib饼图中楔形块的开始处

Pandas—MultiIndex Resample—我不想丢失其他索引的信息´

在numpy数组中寻找楼梯状 struct

如何将一组组合框重置回无 Select tkinter?

如何使用matplotlib查看并列直方图

如何在信号的FFT中获得正确的频率幅值