python - Newspaper3k API 文章下载()失败,HTTPSConnectionPool 端口=443 读取超时。(读取超时 = 7)在 URL 上
问题描述
在 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
我的代码是:
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)
我认为像“renewIPAddress()”这样的东西可能会有所帮助,但我不确定如何将它准确地放入这段代码中。https://stackoverflow.com/a/50496768/2414957
解决方案
你可能已经解决了这个问题。您的代码工作正常,但在某个精确的时刻发生了“读取超时”。我发现 报纸连接偶尔会超时,因为它使用 Python 模块请求。 这些超时通常与您正在查询的源相关联。报纸 3k 确实支持 Config() 中的超时参数,这有助于防止将来出现“读取超时”问题。
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
config.request_timeout = 10
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)
推荐阅读
- javascript - 深度搜索属性值后返回对象
- elasticsearch - Elasticsearch - 如何结合来自多个领域的术语建议?
- python - 检查对象是否可见(使用当前 xlim / ylim)
- r - R-算术不尊重逻辑非运算符和运算顺序?
- python - sqlite3.OperationalError:没有这样的列:错误
- tensorflow - 张量流或pytorch中的分区矩阵乘法
- python - 表单集数据初始化
- javascript - 将音频从 youtube 流式传输到 html 音频标签
- oracle - 有没有办法将 SQL Server 的脚本转换为 Oracle 的脚本
- python-3.x - 如何安装和使用 pandastable ?'griddata' 丢失