天天看點

windows10下使用pyopenssl, pycurl 通路https網站(python)

windows10下使用pyopenssl, pycurl 通路https網站(python)

    python初學者, 想做一個爬蟲, 開始時是爬http的網站, 後來發現那個網站有點惡心, 就改為爬他的https網站版本, 是以就學習下怎麼用python通路https網站

    剛開始時, 以為urllib可以通路https網站, 後來經過實踐和資料的查詢, 發現這是不支援的. 資料推薦我使用pycurl庫.

    本以為就隻需下載下傳pycurl (下載下傳連結看文章最下面) 就好了, 結果發現win10下pycurl-7.21.5.win32-py2.7.exe版本竟然有問題, 展現在perform()不會傳回直到逾時, 後來重新下載下傳pycurl-7.19.5.1.win32-py2.7.exe就可以使用了, 但是此時發現隻能通路http網站,  如果通路https網站會提示如下錯誤:

     c.perform() pycurl.error: (35, 'schannel: next InitializeSecurityContext failed: Unknown error (0x80092012)

     後來上pycurl官網看幫助文檔(看下面  "pucurl安裝官方文檔"  連結)  ,  才大概感覺到需要安裝pyopenssl庫才可以讓pycurl通路https網站 ( 關于這點, 網上沒有類似說法, 都說pycurl可以直接通路, 但是在我系統win10x64上, 的确安裝pyopenssl才可以通路https網站).

     是以就用pip工具下載下傳pyopenssl庫, 使用指令如下: pip install pyopenssl. 

     PS: 關于pip的使用請看

     Python: 删除已安裝的子產品或包 及 python工具pip的安裝和使用            

     http://blog.csdn.net/guyue35/article/details/51337513

     安裝完成pyopenssl和pycurl庫後, 的确可以通路https網站了. 使用如下簡單代碼

#coding:utf-8
import pycurl
url = 'https://www.douban.com/'
c = pycurl.Curl()
c.setopt(c.URL, url)
# Follow redirect.
c.setopt(c.FOLLOWLOCATION, True)
c.perform()
c.close()
exit()      

     并且在使用中發現, pycurl庫通路https網站時候, 不能被HTTP Debugger Pro工具所捕抓到, 而通路http網站就可以捕抓到.

     且pycurl使用時候有時候會出現如下錯誤, 但是再一次運作, 就不報錯, 且成功通路https網站. 原因不解.

     c.perform() pycurl.error: (35, 'schannel: next InitializeSecurityContext failed: Unknown error (0x80092012)

python庫的下載下傳位址

PyPI - the Python Package Index : Python Package Index  

https://pypi.python.org/pypi

python庫中搜尋pycurl的結果

Index of Packages Matching 'pycurl' : Python Package Index  

https://pypi.python.org/pypi?%3Aaction=search&term=PycURL&submit=search

我機子win10x64下可用的pycurl版本

pycurl 7.19.5.1 : Python Package Index  

https://pypi.python.org/pypi/pycurl/7.19.5.1

pucurl官網

PycURL Home Page  

http://pycurl.io/

pucurl安裝官方文檔

PycURL – A Python Interface To The cURL library — PycURL 7.43.0 documentation  

http://pycurl.io/docs/latest/index.html#installation

找到的其他資料

Windows10下安裝OpenSSL_Windows_第七城市  

http://www.th7.cn/system/win/201603/154741.shtml