我是python领域的新手,当我尝试使用python 3练习爬虫时遇到了问题。实际上,由于我的住所,我无法访问某些网站。这意味着以下代码无法正常工作。
import requests
response = requests.get('https://www.google.com')
print(response.status_code)
错误如下所示。
ConnectionError: HTTPSConnectionPool(host='www.google.com', port=443): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x1045892e8>: Failed to establish a new connection: [Errno 60] Operation timed out'))
因此,我使用VPS和SS来完成此操作,之后我可以通过浏览器(Safari和chrome)成功地进行操作。但是,当我打开SS并尝试使用搜寻器时,我失败了。我的代码如下所示,000.000.000.000:000
代表我的VPS的地址和端口。
import requests
url = 'https://www.google.com'
proxies = {
"http": 'http://000.000.000.000:000',
"https": 'https://000.000.000.000:000'
}
response = requests.get(url,proxies=proxies)
print(response.json())
我得到的错误如下所示。
ProxyError: HTTPSConnectionPool(host='www.google.com', port=000): Max retries exceeded with url: / (Caused by ProxyError('Cannot connect to proxy.', RemoteDisconnected('Remote end closed connection without response')))
我认为问题可能是由于缺少标头造成的,所以我编辑了代码,下面显示了其他代码。
import urllib.request as urlreq
import requests
ph = urlreq.ProxyHandler({'https': 'https://000.000.000.000:000'})
oper = urlreq.build_opener(ph)
urlreq.install_opener(oper)
res = requests.get("https://www.google.com", headers={'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.95 Safari/537.36'})
print(res.read())
但是我仍然无法解决此问题,并且在下面显示我得到的错误。
ConnectionError: HTTPSConnectionPool(host='www.google.com', port=000): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x1044f5ba8>: Failed to establish a new connection: [Errno 60] Operation timed out'))
当我打开和关闭SS时,我试图运行上面的代码,但是我仍然无法成功。谁能告诉我问题出在哪里以及如何解决这个问题?