python - 使用 beautifulsoup 美化 html 文档的一部分

标签 python python-3.x beautifulsoup

我有一个 html 文档,我想从中提取表格并美化表格。到目前为止我所拥有的是:

with open('html.txt','r') as file1:
    read_f=file1.read()
soup = BeautifulSoup(read_f)

the_soup=soup.findAll('table', {'id': 'table_id'})
with open('prettified.txt','w') as f2:
    f2.write(the_soup.prettify())

但我收到错误 prettify is no an attribute。

最佳答案

soup.findAll 将返回所有表元素的列表。您应该迭代此列表并打印每个匹配表的美化版本:

with open('prettified.txt','w') as f2:
    for table in the_soup:
        f2.write(table.prettify())

关于python - 使用 beautifulsoup 美化 html 文档的一部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29990366/

相关文章:

python - 从 <a> BeautifulSoup 中提取 href

python - 为什么当我使用 pip install beautifulsoup 时只有 egg-info 而没有实际的模块?

python - kivy 访问 child ID

python - 如何使用 python API 为 Mechanical Turk 中的批处理设置多个资格标准

python-3.x - Django channel 2.0 websocketbridge.js 403 当 self.code(code=1007)

python - 在 Python 中,我们应该在参数列表中设置 Optional[str] = None 吗?

python - 查找元素的上一次出现

python - 在Python中将音频输出从一个函数重定向到另一个函数

python - 如何在 python 列表中搜索多个元素?

python - Python中的第一类函数是什么