错误收集: 1。import urllib.request提示
ImportError: No module named request,我是Python 2.7.3
from urllib import request,提示ImportError: cannot import name request
怎么回事 是python3才有的东东!
Pycharm5.0.3和Python 3.4.4 由于python版本的问题,一些示例需要更改,此代码是参考后修改的。
抓取百度首页:
爬虫学习(一)
import urllib.request
resp=urllib.request.urlopen('http://www.baidu.com')
print(resp.read())
爬虫学习(二)
import urllib.request
url = 'http://www.baidu.com'
req = urllib.request.Request(url)
try:
urllib.request.urlopen(req)
except urllib.request.URLError as e:
if hasattr(e,"code"):
print (e.code)
if hasattr(e,"reason"):
print (e.reason)
else:
print ("OK")
CentOS 7如何将Python 2.X.X升级Python 3.X.X
通过官网https://www.python.org/downloads/下载Python 3.4.3源码; 源码获取命令如下:
wget https://www.python.org/ftp/python/3.4.3/Python-3.4.3.tgz
1、解压Python 3.4.3
tar -xf Python-3.4.3.tgz
2、进入目录: cd Python-3.4.3/
3、安装Python 3.4.3
a、配置./configure
b、编译make
c、安装make install
上面我们已经将Python 3.4.3安装完成,但是我们进入shell后,查看python版本号:
python -V
,发现python还是2.7.5版本,由于将默认的python指向3.4.3以后,yum不能正常使用,需编辑下yum的配置文件:
vi /usr/bin/yum
,这里需要先将原来的python软连接重名
mv /usr/bin/python /usr/bin/python2.7.5
把文件头部的
#!/usr/bin/python改成#!/usr/bin/python2.7.5
保存退出即可; 我们建立一个新的链接:
ln -s /usr/local/bin/python3.4 /usr/bin/python
检验python指向是否成功:python -V