我正在Jupyter Notebook上工作,并且报纸无法从新闻周刊中提取任何内容。我可以让它在 Goose 上运行,但我想有一个备份,以防 Goose 失败。
我尝试过其他网站,如福克斯,雅虎和CNN,所有这些都工作正常。所以《新闻周刊》是一个孤立的问题。
from newspaper import Article
url = 'https://www.newsweek.com/mike-huckabee-blasts-cnns-axelrod-
calling-daughter-trump-press-secretary-sarah-sanders-1444184'
article = Article(url)
article.download()
article.html
article.parse()
article.text
Article `download()` failed with 403 Client Error: Forbidden for url:
https://www.newsweek.com/mike-huckabee-blasts-cnns-axelrod-calling-daughter-
trump-press-secretary-sarah-sanders-1444184 on URL
https://www.newsweek.com/mike-huckabee-blasts-cnns-axelrod-calling-daughter-
trump-press-secretary-sarah-sanders-1444184
您可能已经解决了这个问题,但是当您使用报纸请求文章时,这与未通过用户代理直接相关。
from newspaper import Article
from newspaper import Config
user_agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36'
config = Config()
config.browser_user_agent = user_agent
url = 'https://www.newsweek.com/mike-huckabee-blasts-cnns-axelrod-calling-daughter-trump-press-secretary-sarah-sanders-1444184'
article = Article(url, config=config)
article.download()
article.html
article.parse()
article.text