【问题标题】:Why is.summary on the Python newspaper3k module returning blank?为什么 Python newspaper3k 模块上的 is.summary 返回空白?
【发布时间】:2023-02-24 01:49:49
【问题描述】:
我目前正在编写一个快速的 python 脚本,以使用 newspaper3k 模块来总结给定的新闻文章
以下用于在终端中检索和打印文本的代码工作正常。
import newspaper
# Assign url
url = 'url'
# Extract web data
url_i = newspaper.Article(url="%s" % (url), language='en')
url_i.download()
url_i.parse()
# Display scraped data
print(url_i.text)
然而,当我用“.summary”替换最后一行中的“.text”方法时,没有弹出任何内容,尽管我仍然得到一个代码零,表明编译器没有发现错误
它似乎正在工作,但由于某种原因没有显示。
谢谢。
尝试查看文档和在线,但 .summary 似乎对其他人来说工作得很好。
【问题讨论】:
标签:
python
web-scraping
python-newspaper
newspaper3k
【解决方案1】:
Newspaper3k 有一个特殊的语法来打印文章摘要。
这是来自我的Newspaper3kusage documentation 的示例
from newspaper import Config
from newspaper import Article
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
config.request_timeout = 10
base_url = 'https://www.theguardian.com/news/2020/dec/08/the-curse-of-white-oil-electric-vehicles-dirty-secret-lithium'
article = Article(base_url, config=config)
article.download()
article.parse()
article.nlp()
print(article.summary)
输出:
The sudden excitement surrounding petróleo branco (“white oil”) derives from an invention rarely seen in these parts: the electric car.
More than half (55%) of global lithium production last year originated in just one country: Australia.
The Portuguese government is preparing to offer licences for lithium mining to international companies in a bid to exploit its “white oil” reserves.
As manufacture has slowed down, a glut of lithium on global markets has dampened the white oil boom, if only temporarily.
If people were better informed, he reasoned, it’s just possible that public opinion could swing to their side, and the country’s lithium mining plans could get shelved.