爬取https网站

python2.7

import urllib2
import ssl

weburl = "https://www.douban.com/"
webheader = {
    'Accept': 'text/html, application/xhtml+xml, */*',
    # 'Accept-Encoding': 'gzip, deflate',
    'Accept-Language': 'zh-CN',
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko',
    'DNT': '1',
    'Connection': 'Keep-Alive',
    'Host': 'www.douban.com'
}

context = ssl._create_unverified_context()
req = urllib2.Request(url=weburl, headers=webheader)
webPage = urllib2.urlopen(req, context=context)
data = webPage.read().decode('utf-8')
print data
print type(data)
print type(webPage)
print webPage.geturl()
print webPage.info()
print webPage.getcode()

python 3.6

import urllib.request
import ssl

weburl = "https://www.douban.com/"
webheader = {
    'Accept': 'text/html, application/xhtml+xml, */*',
    # 'Accept-Encoding': 'gzip, deflate',
    'Accept-Language': 'zh-CN',
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko',
    'DNT': '1',
    'Connection': 'Keep-Alive',
    'Host': 'www.douban.com'
}

context = ssl._create_unverified_context()
req = urllib.request.Request(url=weburl, headers=webheader)
webPage = urllib.request.urlopen(req,context=context)
data = webPage.read().decode('utf-8')

print(data)
print(type(webPage))
print(webPage.geturl())
print(webPage.info())
print(webPage.getcode())

用爬虫爬取豆瓣,报错“SSL: CERTIFICATE_VERIFY_FAILED”,Python 升级到 2.7.9 之后引入了一个新特性,当使用urllib.urlopen打开一个 https 链接时,会验证一次 SSL 证书。而当目标网站使用的是自签名的证书时就会抛出此异常。

解决方案有如下两个:

  1)使用ssl创建未经验证的上下文,在urlopen中传入上下文参数

import ssl

context = ssl._create_unverified_context()

webPage = urllib.request.urlopen(req,context=context)

2)全局取消证书验证

import ssl

ssl._create_default_https_context = ssl._create_unverified_context

另外,如果用的是requests模块的get方法,里面有一个verify参数,将其设成False就可以了。

 

解决 'utf-8' codec can't decode byte 0x8b in position 1: invalid start byte  

'Accept-Encoding': 'gzip, deflate',

这条信息代表本地可以接收压缩格式的数据,而服务器在处理时就将大文件压缩再发回客户端,IE在接收完成后在本地对这个文件又进行了解压操作。出错的原因是因为你的程序没有解压这个文件,所以删掉这行就不会出现问题了。

解决InsecureRequestWarning警告

使用请求时,访问的是HTTPS类型的网址,因为HTTPS用了SSL加密,会报这种insecureplatformwarning,平台非安全警告与HTTPS有关,然后查了下解决办法。

直接警告信息的网址  https://urllib3.readthedocs.org/en/latest/security.html

未经证实的制作HTTPS请求被强烈劝阻,但是,如果你了解风险,并希望禁用这些警告,您可以使用disable_warnings()python2.7.6测试无效

>>> import  urllib3 
>>> urllib3.disable_warnings()

限制urllib3所有配置的ssl模块,在某些Python版本(特别是2.7.9之前的)有限制,特别的是,这导致HTTPS请求在本来更多的功能平台成功的变成失败,还有导致某些安全功能模块失效。
如果你遇到这个警告,强烈建议升级到高版本的蟒版本,或者在OpenSSL的/ pyOpenSSL部分描述的,你要使用pyOpenSSL。

继续查找了一下解决办法:

https://github.com/plotly/plotly.py/issues/339

按照文章的解决办法,使用$ pip install requests [security]即可,安装完之后不再有 警告。这个将会安装pyOpenSSL,ndg-httpsclient,pyasn1。

继续查找了一下解决办法python2.7.6测试无效:

from requests.packages.urllib3.exceptions import InsecureRequestWarning,InsecurePlatformWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
requests.packages.urllib3.disable_warnings(InsecurePlatformWarning)
req = requests.get(url, headers=headers, verify=False, timeout=20)

上面的解决方法可能会报错:Can’t connect to HTTPS URL because the SSL module is not available

在requests时无法返回,报错Can’t connect to HTTPS URL because the SSL module is not available

解决办法:

apt-get install libssl-dev
apt-get install openssl
进入到安装的python的目录下
./configure --prefix=/usr/local/python2.7.13 
 make
make install


 

参考资料:

http://blog.csdn.net/Hudeyu777/article/details/76021573

http://blog.csdn.net/liujingclan/article/details/45251915

http://blog.csdn.net/siyitianshi/article/details/45668469

http://blog.csdn.net/yatere/article/details/6619018

http://blog.csdn.net/xiaoxinyu316/article/details/50168331

http://blog.csdn.net/u013630017/article/details/51921144

http://blog.csdn.net/iaiti/article/details/49613097

http://blog.csdn.net/qq_21334991/article/details/79017993

原文地址:https://www.cnblogs.com/domestique/p/8052686.html