如何在不下载文章的情况下使用Newspaper3k库?

时间:2019-06-20 00:50:43

标签: python python-newspaper

假设我有新闻文章的本地副本。我该如何刊登那些文章的报纸?根据文档,报纸库的正常使用如下所示:

from newspaper import Article

url = 'http://fox13now.com/2013/12/30/new-year-new-laws-obamacare-pot-guns-and-drones/'
article.download()
article = Article(url)
article.parse()
# ...

就我而言,我不需要从网页下载文章,因为我已经拥有该页面的本地副本。如何在网页的本地副本上使用报纸?

3 个答案:

答案 0 :(得分:1)

您可以,这有点hacky。例如

import requests
from newspaper import Article

url = 'https://www.cnn.com/2019/06/19/india/chennai-water-crisis-intl-hnk/index.html'

# get sample html
r = requests.get(url)

# save to file
with open('file.html', 'wb') as fh:
    fh.write(r.content)

a = Article(url)

# set html manually
with open("file.html", 'rb') as fh:
    a.html = fh.read()

# need to set download_state to 2 for this to work
a.download_state = 2

a.parse()

# Now the article should be populated
a.text

# 'New Delhi (CNN) The floor...'

download_state来自newspaper.article.py中的摘要:

# /path/to/site-packages/newspaper/article.py
class ArticleDownloadState(object):
    NOT_STARTED = 0
    FAILED_RESPONSE = 1
    SUCCESS = 2

~snip~

# This is why you need to set that variable
class Article:
    def __init__(...):
        ~snip~
         # Keep state for downloads and parsing
        self.is_parsed = False
        self.download_state = ArticleDownloadState.NOT_STARTED
        self.download_exception_msg = None

    def parse(self):
        # will throw exception if download_state isn't 2
        self.throw_if_not_downloaded_verbose()

        self.doc = self.config.get_parser().fromstring(self.html)

作为替代方案,您可以重写该类,使其与parse函数的作用相同:

from newspaper import Article
import io

class localArticle(Article):
    def __init__(self, url, **kwargs):
        # set url to be an empty string in __init__ if it's a file handle
        super().__init__(url if isinstance(url, str) else '', **kwargs)
        # set standalone _url attr so that parse will work as expected
        self._url = url

    def parse(self):

        # sets html and things for you
        if isinstance(self._url, str):
            with open(self._url, 'rb') as fh:
                self.html = fh.read()

        elif isinstance(self._url, io.TextIOWrapper) or isinstance(self._url, io.BufferedReader):
            self.html = self._url.read()

        else:
            raise TypeError(f"Expected file path or file-like object, got {self._url.__class__}")

        self.download_state = 2
        # now parse will continue on with the proper params set
        super(localArticle, self).parse()


a = localArticle('file.html') # pass your file name here
a.parse()

a.text[:10]
# 'New Delhi '

# or you can give it a file handle
with open("file.html", 'rb') as fh:
    a = localArticle(fh)
    a.parse()

a.text[:10]
# 'New Delhi '

答案 1 :(得分:1)

确实有一种官方方法可以解决here

将html加载到程序中后,可以使用set_html()方法将其设置为article.html

import newspaper
with open("file.html", 'rb') as fh:
    ht = fh.read()
article = newspaper.Article(url = ' ')
article.set_html(ht)
article.parse()

答案 2 :(得分:0)

我确定您已经解决了这个问题,但是 Newspaper 可以处理本地存储的HTML文件。

from newspaper import Article

# Downloading the HTML for the article
url = 'http://fox13now.com/2013/12/30/new-year-new-laws-obamacare-pot-guns-and-drones/'
article = Article(url)
article.download()
article.parse()
with open('fox13no.html', 'w') as fileout:
   fileout.write(article.html)

# Read the locally stored HTML with Newspaper
with open("fox13no.html", 'r') as f:
   # note the URL string is empty
   article = Article('', language='en')
   article.download(input_html=f.read())
   article.parse()
   print(article.title) 
   New Year, new laws: Obamacare, pot, guns and drones