请求g的Python错误

2024-04-25 20:57:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试编写一个Python脚本,它使我能够访问一个网页并从该页面下载一个文件。我的第一次尝试就是直接进入该页面,然后尝试了以下代码:

import requests

url = 'https://www.google.com/?gws_rd=ssl' #using google as an example

r = requests.get(url)

print(r.url)

我得到了这个错误:

^{pr2}$

有人能帮帮我吗?在


Tags: 文件代码httpsimport脚本comurl网页
3条回答

你还在为请求烦恼吗

from urllib2 import urlopen
u = urlopen("https://www.google.com/?gws_rd=ssl")
data = u.read()
u.close()

也许这会管用。在

你得到这个错误是因为远程端(这里是Google)正在关闭你的请求,或者你无法再与它建立连接。在

从错误中:

ConnectionError: HTTPSConnectionPool(host='www.google.com', port=443): 
Max retries exceeded with url: /?gws_rd=ssl 
(Caused by <class 'socket.error'>: [Errno 10054] An existing connection was forcibly closed by the remote host)

我们可以查看source中的提示:

^{pr2}$

尝试另一个主机示例,您的代码应该可以工作,例如https://example.org。在

来自您的操作系统(Windows)的错误消息“现有连接已被远程主机强制关闭”,请求显示此文本,以帮助您。在

你的代码没问题。在

我想这个问题是由url = 'https://www.google.com/?gws_rd=ssl'引起的。在

可能您的网络无法访问www.google.com,请尝试其他url。在

相关问题 更多 >