我已经Newspapper3k在我的 Mac 上安装了 Lib sudo pip3 install Newspapper3k。我使用 Python 3。我想返回 Article 对象支持的数据,即 url、日期、标题、文本、摘要和关键字,但我没有得到任何数据:
import newspaper
from newspaper import Article
#creating website for scraping
cnn_paper = newspaper.build('https://www.euronews.com/', memoize_articles=False)
#I have tried for https://www.euronews.com/, https://edition.cnn.com/, https://www.bbc.com/
for article in cnn_paper.articles:
article_url = article.url #works
news_article = Article(article_url)#works
print("OBJECT:", news_article, '\n')#works
print("URL:", article_url, '\n')#works
print("DATE:", news_article.publish_date, '\n')#does not work
print("TITLE:", news_article.title, '\n')#does not work
print("TEXT:", news_article.text, '\n')#does not work
print("SUMMARY:", news_article.summary, '\n')#does not work
print("KEYWORDS:", news_article.keywords, '\n')#does not work
print() …Run Code Online (Sandbox Code Playgroud) 在 Firefox 中浏览时,我可以看到http://www.chicagotribune.com/ct-florida-school-shooter-nikolas-cruz-20180217-story.html 。但是,newspaper3k给了我这个错误:
Article download() failed with HTTPSConnectionPool(host='www.chicagotribune.com', port=443): Read timed out. (read timeout=7) on URL http://www.chicagotribune.com/ct-florida-school-shooter-nikolas-cruz-20180217-story.html
Run Code Online (Sandbox Code Playgroud)
我的代码是:
from newspaper import Article
from newspaper import Config
user_agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:78.0) Gecko/20100101 Firefox/78.0'
config = Config()
config.browser_user_agent = user_agent
url = "https://www.chicagotribune.com/nation-world/ct-florida-school-shooter-nikolas-cruz-20180217-story.html"
page = Article(url, config=config)
page.download()
page.parse()
print(page.text)
Run Code Online (Sandbox Code Playgroud)
我认为像“renewIPAddress()”之类的东西可能会有所帮助,但我不确定如何准确地将其适合此代码。/sf/answers/3534773791/