LookupError: ********************************************************************** Resource punkt not found. Please use the NLTK Downloader to obtain the resource: >>> import nltk >>> nltk.download('punkt') Attempted to load tokenizers/punkt/english.pickle Searched in: - '/home/a/nltk_data' - '/home/a/anaconda3/envs/py2/nltk_data' - '/home/a/anaconda3/envs/py2/share/nltk_data' - '/home/a/anaconda3/envs/py2/lib/nltk_data' - '/usr/share/nltk_data' - '/usr/local/share/nltk_data' - '/usr/lib/nltk_data' - '/usr/local/lib/nltk_data' - u'' **********************************************************************
import nltk import ssl try: _create_unverified_https_context = ssl._create_unverified_context except AttributeError: pass else: ssl._create_default_https_context = _create_unverified_https_context nltk.download("punkt")
補充材料:python
SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:581)react
今天想試用一下百度的語音識別API,附帶步驟:api
1. 先去百度開放雲平臺註冊,成爲開發者,審覈可能須要時間的,我去年申過如今帳號還在post
2. 而後建立一個應用測試
3.爲建立完的應用添加服務,有倆,語音識別和語音生成this
4. 這樣我就有一個調用他語音識別接口的access_token了,這個token因爲我採用的是API For Rest,要拿API_key和secret_key經過一個http請求得到,問題就出在這兒了url
我用request按照他文檔的樣子Post了一下,又Get了一下都報一個驗證失敗的錯誤。spa
requests.post('https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id=xxxxxxx&client_secret=xxxxxxx').content
.net
requests.get('https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id=xxxxxxx&client_secret=xxxxxxx').contentrest
他告訴我:
SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:581)
找了一下,有人說緣由是這樣的:
Python 2.7.9 以後引入了一個新特性
當你urllib.urlopen一個 https 的時候會驗證一次 SSL 證書
當目標使用的是自簽名的證書時就會爆出一個
urllib2.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:581)> 的錯誤消息
確實我用urllib試了一下結果同樣,requests跟urllib是同樣的。
那麼要解決這個問題,PEP-0476的文檔說
For users who wish to opt out of certificate verification on a single connection, they can achieve this by providing the contextargument to urllib.urlopen :
import ssl
# This restores the same behavior as before.
context = ssl._create_unverified_context()
urllib.urlopen("https://no-valid-cert", context=context)
It is also possible, though highly discouraged , to globally disable verification by monkeypatching the ssl module in versions of Python that implement this PEP:
import ssl
try:
_create_unverified_https_context = ssl._create_unverified_context
except AttributeError:
# Legacy Python that doesn't verify HTTPS certificates by default
pass
else:
# Handle target environment that doesn't support HTTPS verification
ssl._create_default_https_context = _create_unverified_https_context
就是說你能夠禁掉這個證書的要求,urllib來講有兩種方式,一種是urllib.urlopen()有一個參數context,把他設成ssl._create_unverified_context或者修改如今的全局默認值
_create_unverified_https_context
或
ssl._create_default_https_context
爲
ssl._create_unverified_context
測試了一下,確實能夠,返回了幾個token,那麼requests呢,難道必須設置全局變量嗎。其實request的post和get都有一個叫verify的參數,把他設成False就能夠了。
print requests.get('https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id=xxxxx&client_secret=xxxxxxxx', verify=False).content