【问题标题】:prettifying a part of the html doc using beautifulsoup使用 beautifulsoup 美化 html 文档的一部分
【发布时间】:2015-05-01 15:55:39
【问题描述】:

我有一个 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。

【问题讨论】:

  • 是否有多个'table_id'
  • 或者它应该是类。然而,我正在阅读具有相同 id 的凌乱 html 表格。

标签: python python-3.x beautifulsoup


【解决方案1】:

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

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

【讨论】:

  • 哦,这就是我卡住的地方。谢谢!
猜你喜欢
  • 2021-12-03
  • 1970-01-01
  • 2022-10-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-31
  • 2022-01-20
相关资源
最近更新 更多