爬取https网站

python2.7html

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.6python

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 证书。而当目标网站使用的是自签名的证书时就会抛出此异常。git

解决方案有以下两个:github

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

import ssl安全

context = ssl._create_unverified_context()服务器

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

2)全局取消证书验证python2.7

import sslide

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

相关文章
相关标签/搜索