Python:如何使用urllib2访问https
怪我咯
怪我咯 2017-04-17 13:57:15
0
2
426

想要访问https网站,获取网页的源代码,如:https://www.baidu.com
源码如下:

f = urllib2.urlopen("https://www.baidu.com/")
buf = f.read()
f.close()

结果报错:
urllib2.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:581)>

Python版本:2.7.9
运行平台:Windows

怪我咯
怪我咯

走同样的路,发现不同的人生

全部回覆(2)
Peter_Zhu
>>> import urllib2
>>> f = urllib2.urlopen("https://www.baidu.com/")
>>> buf = f.read()
>>> f.close()
>>> buf
'<html>\r\n<head>\r\n\t<script>\r\n\t\tlocation.replace(location.href.replace("https://","http://"));\r\n\t</script>\r\n</head>\r\n<body>\r\n\t<noscript><meta http-equiv="refresh" content="0;url=http://www.baidu.com/"></noscript>\r\n</body>\r\n</html>'
>>>

在Mac下運作是正常的,應該是你的環境下不支援SSL,Windows環境需要自行安裝

Note HTTPS support is only available if the socket module was compiled with SSL support.

附上安裝方法,希望能對你有幫助:
How to install Python ssl module on Windows?

巴扎黑

https 訪問,使用pycurl更方便一些~

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!