nltk 的分词器punkt: ssl问题没法下载

 

 报错:
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 

相关文章
相关标签/搜索