【问题标题】:Publishing date in newspaper library always returning None报纸图书馆的出版日期总是返回 无
【发布时间】:2016-01-09 04:58:17
【问题描述】:

我最近一直在使用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)

我很确定我正在这样做。我不确定是否有人注意到我的问题。

【问题讨论】:

  • 您遇到问题的网址是什么?
  • 所有网址均未返回任何发布日期。
  • @Eigenvalue 不要忘记article.parse() 之前article.publish_date
  • @Eigenvalue 哦,我认为你有一个订单问题,所以将article.publish_date 值分配给__init__ 中的一个实例变量并在任何你想要的地方使用它。
  • 我在 date_made 函数中做了这个。为什么它必须在 init 中?

标签: python datetime python-newspaper


【解决方案1】:

我 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

如果 newspaper 确实在 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 新闻将他们的日期存储在元标记部分,但在 newspaper 不查询的标记中。要从 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'}

有时,来源的发布日期位于报纸不查看的部分。发生这种情况时,您必须在 newspaper 周围添加一些额外的代码来获取日期。

例如 BBC 将其日期存储在脚本 application/ld+json 中。 Newspaper 并非设计用于查询或提取此脚本。要从 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']

我在 GitHub 上发布了Newspaper Usage Document,讨论了各种收集策略以及围绕该库的其他主题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-05
    • 2013-10-31
    相关资源
    最近更新 更多