我正在尝试使用 googlesearch 和报纸 3k python 包的组合来获取文章列表。使用 article.parse 时,我最终得到一个错误:paper.article.ArticleException: Article download()
failed with 403 Client Error: Forbidden for url: https://www.newsweek.com/donald-trump-hillary-clinton-2020- URL https://www.newsweek.com/donald-trump-hillary-clinton-2020-rally-orlando-1444697上的 rally-orlando-1444697
我尝试在执行脚本时以管理员身份运行,并且在浏览器中直接打开时链接有效。
这是我的代码:
import googlesearch
from newspaper import Article
query = "trump"
urlList = []
for j in googlesearch.search_news(query, tld="com", num=500, stop=200, pause=.01):
urlList.append(j)
print(urlList)
articleList = []
for i in urlList:
article = Article(i)
article.download()
article.html
article.parse()
articleList.append(article.text)
print(article.text)
这是我的完整错误输出:
Traceback (most recent call last):
File "C:/Users/andre/PycharmProjects/StockBot/WebCrawlerTest.py", line 31, in <module>
article.parse()
File "C:\Users\andre\AppData\Local\Programs\Python\Python37\lib\site-packages\newspaper\article.py", line 191, in parse
self.throw_if_not_downloaded_verbose()
File "C:\Users\andre\AppData\Local\Programs\Python\Python37\lib\site-packages\newspaper\article.py", line 532, in throw_if_not_downloaded_verbose
(self.download_exception_msg, self.url))
newspaper.article.ArticleException: Article `download()` failed with 403 Client Error: Forbidden for url: https://www.newsweek.com/donald-trump-hillary-clinton-2020-rally-orlando-1444697 on URL https://www.newsweek.com/donald-trump-hillary-clinton-2020-rally-orlando-1444697
我希望它只输出文章的文本。你能提供的任何帮助都会很棒。谢谢!