爬取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

相關文章
相關標籤/搜索