Sea*_*ean 5 python datetime web-scraping
我想抓取网络上发布的新闻文章的确切发布时间。
有些网页有漂亮且格式化的标题,我可以在其中提取“上次修改”或“发布日期”,标题中的信息很混乱,但可用。(顺便说一句,metadata_parser有很大帮助!)
但是像 BBC 和 CNN 这样的大型新闻机构不会在 html 标题中放置日期和时间信息。所以我试图从 html 代码中获取日期和发布时间。
对于 BBC,日期时间嵌入如下:
<div data-timestamp-inserted="true" class="date date--v2" data-seconds="1447658338" data-datetime="16 November 2015">16 November 2015</div>
Run Code Online (Sandbox Code Playgroud)
对于 CNN,它就像:
<p class="update-time">Updated 0137 GMT (0937 HKT) November 16, 2015 <span id="js-pagetop_video_source" class="video__source top_source">| Video Source: <a href="http://www.cnn.com/">CNN</a></span></p>
Run Code Online (Sandbox Code Playgroud)
对于纽约时报,
<p class="byline-dateline"><span class="byline" itemprop="author creator" itemscope="" itemtype="http://schema.org/Person">By <span class="byline-author" data-byline-name="AURELIEN BREEDEN" itemprop="name">AURELIEN BREEDEN</span>, </span><span class="byline" itemprop="author creator" itemscope="" itemtype="http://schema.org/Person"><span class="byline-author" data-byline-name="KIMIKO DE FREYTAS-TAMURA" itemprop="name">KIMIKO DE FREYTAS-TAMURA</span> and </span><span class="byline" itemprop="author creator" itemscope="" itemtype="http://schema.org/Person" itemid="http://topics.nytimes.com/top/reference/timestopics/people/b/katrin_bennhold/index.html"><a href="http://topics.nytimes.com/top/reference/timestopics/people/b/katrin_bennhold/index.html" rel="author" title="More Articles by KATRIN BENNHOLD"><span class="byline-author" data-byline-name="KATRIN BENNHOLD" itemprop="name">KATRIN BENNHOLD</span></a></span><time class="dateline" datetime="2015-11-16" itemprop="datePublished" content="2015-11-16">NOV. 16, 2015</time></p>
Run Code Online (Sandbox Code Playgroud)
可以看出,几乎每个新闻机构都有自己的方式在网页中放置数据和时间。
我的问题是,是否可以使用BeautifulSoup 中的某种模糊搜索和某种包来提取日期时间信息,这样我就不必为每个网站编写规则?
谢谢!
根据我的经验和拙见,抓取通用信息的最佳方法是使用NER(命名实体识别)系统。
我建议使用 Scrapinghub 的webstruct库:
Webstruct 是一个用于创建处理 HTML 数据的统计 NER 系统的库,即用于构建从网页中提取命名实体(地址、组织名称、开放时间等)的工具的库。
与大多数 NER 系统不同,webstruct 适用于 HTML 数据,而不仅仅是文本数据。这允许定义使用 HTML 结构的功能,也可以将注释结果嵌入回 HTML 中。
Github 存储库:https://github.com/scrapinghub/webstruct
文档: http: //webstruct.readthedocs.org/en/latest/
更新:
当您需要抓取日期时,您还可以使用Dateparser:
dateparser 提供的模块可以轻松解析网页上常见的几乎所有字符串格式的本地化日期。
Github 存储库:https ://github.com/scrapinghub/dateparser
文档: https: //dateparser.readthedocs.org/en/latest/