【问题标题】:Deleting all data between two html tags in python删除python中两个html标签之间的所有数据
【发布时间】:2012-11-19 02:25:08
【问题描述】:

我正在制作一个网络爬虫,我想完全删除一些 div,因为我对数据的分析不需要它们。 我正在使用 Beautiful Soup 来解析数据,但我无法弄清楚如何完全消除 div

【问题讨论】:

  • 这可能会有所帮助:stackoverflow.com/questions/2649751/…
  • With lxml.html: from lxml import html doc = html.fromstring(input) for el in doc.cssselect('div.comment'): el.drop_tree() result = html.tostring(文档)

标签: python html web-scraping beautifulsoup


【解决方案1】:

你可以使用类似下面的东西:

>>> import bs4
>>> blah = '<div id="test"><p>one</p></div><div id="okay"><p>something</p></div>'
>>> soup = bs4.BeautifulSoup(blah)
>>> soup('div', {'id': 'test'})[0].extract()
<div id="test"><p>one</p></div>
>>> soup
<html><body><div id="okay"><p>something</p></div></body></html>

【讨论】:

    猜你喜欢
    • 2015-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-05
    • 1970-01-01
    • 1970-01-01
    • 2015-07-07
    • 1970-01-01
    相关资源
    最近更新 更多