【问题标题】:Remove Duplicate Substrings/Elements from Scraped HTML?从抓取的 HTML 中删除重复的子字符串/元素?
【发布时间】:2015-06-25 07:48:02
【问题描述】:

我有一大堆从 Kindle 书籍中提取的 HTML。而且它有很多重复的元素和重复的子字符串。

长话短说,Kindle DRM 删除了我 90% 的注释,我使用它没有删除的位置数据将其全部恢复。但亚马逊的位置数据有些不精确(对应 150 字节的块),所以我最终得到了很多冗余。

例子:

<html>
 <body>
  <p>
   aesar”), at the Battle of Pavia (1525).
  </p>
  <div height="0em">
  </div>
  <mbp:pagebreak>
  </mbp:pagebreak>
  <a id="filepos97755">
  </a>
  <h1 align="center" height="2em">
   <font size="5">
    <b>
     KNOW WHEN
     <br/>
     TO RETIRE
    </b>
   </font>
  </h1>
  <div height="3em">
  </div>
  <p align="justify" height="0em" width="1em">
  </p>
 </body>
</html>

<html>
 <body>
  <h1 align="center" height="2em">
   <font size="5">
    <b>
     KNOW WHEN
     <br/>
     TO RETIRE
    </b>
   </font>
  </h1>
  <div height="3em">
  </div>
  <p align="justify" height="0em" width="1em">
   Anything in motion must wax and wane. Some speak of states of movement, but they are anything but static.
  </p>
  <div height="0em">
  </div>
  <p height="0em">
  </p>
 </body>
</html>



<html>
 <body>
  <p align="justify" height="0em" width="1em">
   Anything in motion must wax and wane. Some speak of states of movement, but they are anything but static.
  </p>
  <div height="0em">
  </div>
  <p align="justify" height="0em" width="1em">
   It takes great foresight to predict the decline of a restless, relentless wheel. The sharpest gamblers know when to quit
  </p>
 </body>
</html>

有没有人知道什么会有所帮助?

【问题讨论】:

标签: python html parsing screen-scraping redundancy


【解决方案1】:

天哪,真是一团糟。从您展示的一小部分输出看来,重要的东西似乎在段落标签中。我会使用漂亮的 python (http://www.crummy.com/software/BeautifulSoup/bs4/doc/) 汤从&lt;P&gt; 标签中提取所有信息,然后删除多余的信息。如果您还想保留其他格式,那将是一个熊。回去后我会尝试使用漂亮的汤,并确信我无法以更好的格式导出它。

【讨论】:

  • 这正是我所做的,而且效果很好。我根据运行列表检查了每个段落/h1 标签,结果没有冗余!
猜你喜欢
  • 2021-08-30
  • 2023-04-09
  • 1970-01-01
  • 2018-02-20
  • 1970-01-01
  • 2018-09-08
  • 2014-01-27
  • 2019-03-14
  • 1970-01-01
相关资源
最近更新 更多