簡體   English   中英

報庫中的發布日期總是返回無

[英]Publishing date in newspaper library always returning None

我最近一直在使用報紙圖書館。 我發現的唯一問題是當我做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']

我在 GitHub 上發布了一份報紙使用文檔,討論了圍繞這個庫的各種收集策略和其他主題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM