python - 报库中的发布日期总是返回无

标签 python datetime python-newspaper

我一直在用 newspaper最近图书馆。我发现的唯一问题是我何时做 article.publish_date我总是收到 None .

class NewsArticle:
    def __init__(self,url):
        self.article = Article(url)
        self.article.download()
        self.article.parse()
        self.article.nlp()

    def getKeywords(self):
        x = self.article.keywords
        for i in range(0,len(x)):
            x[i] = x[i].encode('ascii', 'ignore')
        return x

        return self.article.keywords

    def getSummary(self):
        return self.article.summary.encode('ascii', 'ignore')

    def getAuthors(self):
        x = self.article.authors
        for i in range(0,len(x)):
            x[i] = x[i].encode('ascii', 'ignore')
        return x

    def thumbnail_url(self):
        return self.article.top_image.encode('ascii', 'ignore')

    def date_made(self):
        print self.article.publish_date
        return self.article.publish_date
    def get_videos(self):
        x=self.article.movies
        for i in range(0,len(x)):
            x[i] = x[i].encode('ascii', 'ignore')
        return x
    def get_title(self):
        return self.article.title.encode('ascii','ignore')

我正在浏览一堆 URL。你可以看到我正在打印 publish_date在归还之前。

我得到了我之前说的:
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None

所有其他功能都按预期工作。该站点的文档查看了一个示例,
>>> article.publish_date
datetime.datetime(2013, 12, 30 0, 0)

我正在做这个我很确定。我不确定是否有人看到我的问题。

最佳答案

我 100% 确定您在过去 5 年里已经解决了这个问题,但我想在报纸上发表我的知识。
这个 Python 库并不完美,因为它旨在尽最大努力收集特定元素,例如文章标题、作者姓名、发布日期和其他几个项目。即使尽了最大努力,报纸也会错过不在其设计位置上的内容。
例如,这是来自报纸的提取代码。

3 strategies for publishing date extraction. The strategies are descending in accuracy and the next strategy is only attempted if a preferred one fails.

1. Pubdate from URL
2. Pubdate from metadata
3. Raw regex searches in the HTML + added heuristics
如果报纸确实在 URL 中找到了日期,它就会移动到元标记,但只有这些:
PUBLISH_DATE_TAGS = [
            {'attribute': 'property', 'value': 'rnews:datePublished',
             'content': 'content'},
            {'attribute': 'property', 'value': 'article:published_time',
             'content': 'content'},
            {'attribute': 'name', 'value': 'OriginalPublicationDate',
             'content': 'content'},
            {'attribute': 'itemprop', 'value': 'datePublished',
             'content': 'datetime'},
            {'attribute': 'property', 'value': 'og:published_time',
             'content': 'content'},
            {'attribute': 'name', 'value': 'article_date_original',
             'content': 'content'},
            {'attribute': 'name', 'value': 'publication_date',
             'content': 'content'},
            {'attribute': 'name', 'value': 'sailthru.date',
             'content': 'content'},
            {'attribute': 'name', 'value': 'PublishDate',
             'content': 'content'},
            {'attribute': 'pubdate', 'value': 'pubdate',
             'content': 'datetime'},
            {'attribute': 'name', 'value': 'publish_date',
             'content': 'content'},
Fox news 将他们的日期存储在元标签部分,但在报纸不查询的标签中。要从 Fox 新闻文章中提取日期,您可以这样做:
article_meta_data = article.meta_data

article_published_date = str({value for (key, value) in article_meta_data.items() if key == 'dcterms.created'})
print(article_published_date)
{'2020-10-11T12:51:53-04:00'}
有时,一个消息来源在报纸没有看到的部分中有其发布日期。发生这种情况时,您必须在报纸周围包裹一些额外的代码来获取日期。
例如 BBC 将其日期存储在脚本 application/ld+json 中。报纸不是为了从这个脚本中查询或提取而设计的。要从 BBC 文章中提取日期,您可以这样做:
soup = BeautifulSoup(article.html, 'html.parser')
bbc_dictionary = json.loads("".join(soup.find("script", {"type":"application/ld+json"}).contents))

date_published = [value for (key, value) in bbc_dictionary.items() if key == 'datePublished']
print(date_published)
['2020-10-11T20:11:33.000Z']
我发表了一篇 Newspaper Usage Document在 GitHub 上讨论了围绕这个库的各种收集策略和其他主题。

关于python - 报库中的发布日期总是返回无,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33068841/

相关文章:

python - 使用报纸从 HTML 中提取图像

python - 使用 Python 中的 NewsPaper 库将新闻文章抓取到一个列表中?

Python:获取两个点数字之间的范围

python - 在 Pyramid 中使用 babel 和 lingua

php - "It is not safe to rely on the system' s 时区设置背后的基本原理是什么”?

python - 如何将增量添加到 python datetime.time?

python - 限制报纸的 URL 输出

python - 如何在 Matplotlib 中使用 3D 数据(给定的 x、y 和 z 数组)和颜色编码的 Z 轴表示来绘制 2D 图形?

python - 修补 python eventlet 以解决 Ubuntu 12.04 中的 OpenSSL TLS 1.1 错误

csv - Bigquery 日期时间格式 csv 到 bigquery YYYY-MM-DD HH :MM[:SS[. SSSSSS]]