1

我正在使用 Jupyter Notebook 并遇到报纸问题,无法从新闻周刊中删除任何内容。我可以让它在 Goose 上运行,但我想有一个备份,以防 Goose 失败。

我尝试过其他网站,例如 Fox、Yahoo 和 CNN,所有这些都运行良好。所以 NewsWeek 是一个孤立的问题。

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
4

1 回答 1

1

您可能已经解决了这个问题,但是当您使用Newspaper请求文章时,它与不传递用户代理直接相关。

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
于 2020-09-29T21:53:43.967 回答