【发布时间】:2017-11-24 19:20:47
【问题描述】:
我得到 urllib2.URL错误: 调用 mechanize.browser.open('my https site') 时出错。
我在网上搜索过,但对我没有任何帮助。
这是我的代码:
import ssl
try:
_create_unverified_https_context = ssl._create_unverified_context
except AttributeError:
# Legacy Python that doesn't verify HTTPS certificates by default
pass
else:
# Handle target environment that doesn't support HTTPS verification
ssl._create_default_https_context = _create_unverified_https_context
import mechanize
import operator
from bs4 import BeautifulSoup
import os
myBrowser = mechanize.Browser()
myBrowser.set_handle_robots(False)
myBrowser.set_handle_refresh(False)
myBrowser.open("https://uwp.puchd.ac.in/common/viewmarks.aspx")
这是我得到的错误:
Traceback (most recent call last):
File "C:/Users/Himanshu/Desktop/UIET Rank system.py", line 27, in <module>
myBrowser.open("https://uwp.puchd.ac.in/common/viewmarks.aspx")
File "C:\Python27\lib\site-packages\mechanize\_mechanize.py", line 254, in open
return self._mech_open(url_or_request, data, timeout=timeout)
File "C:\Python27\lib\site-packages\mechanize\_mechanize.py", line 284, in _mech_open
response = UserAgentBase.open(self, request, data)
File "C:\Python27\lib\site-packages\mechanize\_opener.py", line 195, in open
response = urlopen(self, req, data)
File "C:\Python27\lib\site-packages\mechanize\_urllib2_fork.py", line 352, in _open
'_open', req)
File "C:\Python27\lib\site-packages\mechanize\_urllib2_fork.py", line 340, in _call_chain
result = func(*args)
File "C:\Python27\lib\site-packages\mechanize\_urllib2_fork.py", line 1215, in https_open
return self.do_open(conn_factory, req)
File "C:\Python27\lib\site-packages\mechanize\_urllib2_fork.py", line 1160, in do_open
raise URLError(err)
urllib2.URLError: <urlopen error EOF occurred in violation of protocol (_ssl.c:661)>
Process finished with exit code 1
其他信息:
import ssl
print ssl.OPENSSL_VERSION
output>> OpenSSL 1.0.2j 26 Sep 2016
python版本
Python 2.7.13 (v2.7.13:a06454b1afa1, Dec 17 2016, 20:42:59) [MSC v.1500 32 bit (Intel)] on win32
有没有办法绕过这个错误?
注意:
- 我只想使用 mechanize,因为我的应用程序已经准备好并且 一年前可以工作,但现在不行了,我不想再更改整个代码。
- 我在 Windows 上使用 Pycharm。
- 请尝试打开this webpage which I am trying to open 它在chrome中也显示“不安全的连接”,我们需要继续访问网页。这可能是问题所在。另外,我没有此网页的证书。
- 我的应用程序与安全性无关,因此将 SSL 验证设置为 false(我尝试从其他帖子中这样做,但它对我不起作用)或类似的东西会非常好。唯一的目标是应用程序应该工作。
【问题讨论】:
标签: python ssl urllib2 mechanize