初学Python遇到的坑
2024-09-04 15:26:30
问题一
脚本内容
MacBookPro:Desktop mac$ cat wike.py
#!/usr/bin/python
from urllib.request import urlopen
from bs4 import BeautifulSoup
#import ssl
#ssl._create_default_https_context = ssl._create_unverified_context
html = urlopen("http://en.wikipedia.org/wiki/Kevin_Bacon")
bsObj = BeautifulSoup(html,"lxml")
for link in bsObj.findAll("a"):
if 'href' in link.attrs:
print(link.attrs['href'])
输出
报错
MacBookPro:Desktop mac$ python3 wike.py
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/urllib/request.py", line 1254, in do_open
h.request(req.get_method(), req.selector, req.data, headers)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/http/client.py", line 1106, in request
self._send_request(method, url, body, headers)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/http/client.py", line 1151, in _send_request
self.endheaders(body)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/http/client.py", line 1102, in endheaders
self._send_output(message_body)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/http/client.py", line 934, in _send_output
self.send(msg)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/http/client.py", line 877, in send
self.connect()
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/http/client.py", line 1260, in connect
server_hostname=server_hostname)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/ssl.py", line 377, in wrap_socket
_context=self)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/ssl.py", line 752, in __init__
self.do_handshake()
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/ssl.py", line 988, in do_handshake
self._sslobj.do_handshake()
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/ssl.py", line 633, in do_handshake
self._sslobj.do_handshake()
ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:645)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "wike.py", line 6, in <module>
html = urlopen("http://en.wikipedia.org/wiki/Kevin_Bacon")
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/urllib/request.py", line 163, in urlopen
return opener.open(url, data, timeout)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/urllib/request.py", line 472, in open
response = meth(req, response)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/urllib/request.py", line 582, in http_response
'http', request, response, code, msg, hdrs)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/urllib/request.py", line 504, in error
result = self._call_chain(*args)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/urllib/request.py", line 444, in _call_chain
result = func(*args)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/urllib/request.py", line 696, in http_error_302
return self.parent.open(new, timeout=req.timeout)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/urllib/request.py", line 466, in open
response = self._open(req, data)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/urllib/request.py", line 484, in _open
'_open', req)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/urllib/request.py", line 444, in _call_chain
result = func(*args)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/urllib/request.py", line 1297, in https_open
context=self._context, check_hostname=self._check_hostname)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/urllib/request.py", line 1256, in do_open
raise URLError(err)
urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:645)>
MacBookPro:Desktop mac$ echo $?
1
出了一堆的错,然后在 http://stackoverflow.com/questions/27835619/ssl-certificate-verify-failed-error 找到了答案:
要引入 一个ssl 的模块,然后,就没有然后了
问题二
Could not fetch URL https://pypi.python.org/simple/nltk/: There was a problem confirming the ssl certificate: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:749) - skipping
Could not find a version that satisfies the requirement nltk (from versions: )
No matching distribution found for nltk
报了上面的错,下面的语句解决.
pip --trusted-host pypi.python.org install
问题三
<urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:645
报了上面的错,下面的语句解决
pip3 install certifi
最新文章
- 为什么 NaN 不等于自身?
- SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.
- uploadfile图片上传和ashx
- 通过串口设备vid,pid自动获得该设备所对应的串口号
- 【大数处理、正则表达式】NYOJ-513
- Linux 平台下 YUM 源配置 手册
- Linux中的栈:用户态栈/内核栈/中断栈
- selenium webdriver启动Chrome浏览器后无法输入网址的解决办法
- UILabel 的使用,属性详解
- jQuery选取和操纵元素的特点
- Mysql字符集修改为UTF8
- 2016大连网络赛 Football Games
- oracle 数据库中的序列
- 使用配置文件自定义Ribbon配置
- Ubuntu中安装和配置 Java JDK,并卸载自带OpenJDK(以Ubuntu 14.04为例)
- docker cgroup技术之cpu和cpuset
- jquery-easyui:格式化列
- Vue项目中如何引用外部js
- 55. Jump Game(贪心)
- 疯狂JAVA——第五章 面向对象(上)