Linux-Ubuntu lxml库导入失败 解决方法

问题描述

一开始是报这个错,也就是找不到lxml这个包。
bs4.FeatureNotFound: Couldn't find a tree builder with the features you requested: lxml. Do you need to install a parser library?
因为我这个python程序是在windows运行好好的,放到服务器上说少包。

解决方法

  1. 错误方法
    首先 sudo apt-get install python3-lxml 不管用,报找不到包。然后看其他人的博客知道是因为源的问题,我替换了镜像源代码是: sudo vim /etc/apt/sources.list,用的阿里的,把内容文件内容注释掉
deb https://mirrors.aliyun.com/ubuntu/ trusty main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ trusty main restricted universe multiverse
deb https://mirrors.aliyun.com/ubuntu/ trusty-security main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ trusty-security main restricted universe multiverse

deb https://mirrors.aliyun.com/ubuntu/ trusty-updates main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ trusty-updates main restricted universe multiverse

deb https://mirrors.aliyun.com/ubuntu/ trusty-backports main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ trusty-backports main restricted universe multiverse

然后呢还是不行,我又看到要用pip,然后安装pip,sudo apt-get install python3-pip。安装好了之后,pip3 install -i 阿里的镜像地址 lxml
依旧是不行,然后我注意到是不是操作系统和python的事。有一篇文章是说操作系统是linux64位,python软件是32位的,所以需要32位的lxml包,然后去官网下载。
这个去网上自己排除就可以,其实大部分应该不是这个问题。

还有就是下载lxml的时候版本还是有讲究的,下载地址
需要看你的pip的版本是多少,然后参考这个文章看自己应该下载多少版本号:manylinux1和其他的区别
我下载的是这个:
在这里插入图片描述
cp36的意思是python版本为3.6,我的pip版本是9.0的,所以选manylinux1,64位的python版本。
下载后,你的python先安装wheel,pip3 install wheel,这个是用于解压这个轮子的。
最后我发现,这种第三方库,你安装到python的包的根目录是没用的,应该放在和运行程序在同一文件夹才行,不然我发现程序找不到。因为中间经历过长达几个小时的,lxml安装了但还是程序说找不到lxml。真的会谢…
pip3 install lxml-4.9.0-cp36-cp36m-manylinux_2_5_x86_64.manylinux1_x86_64.whl,最后发现他还是提示我我已经安装了。
2. 最后的解决方法
我把Python包的根目录的lxml文件复制到了程序文件夹里,结果完美运行。
cp -r /usr/local/lib/python3.6/dist-packages ././代表当前文件夹

  1. 当然其中还有一个方式,就是舍弃用lxml,用html.parser。但是效率会降低,
    soup = BeautifulSoup(html, 'lxml') 改为 soup = BeautifulSoup(html, 'html.parser')

真是鼓捣了一下午,看了各种各样别人写的博客。最后发现自己的问题太低级了…

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值