欢迎来到李先生的博客

深山的鹿,不知归处;万般皆苦,只可自渡。
扩大
缩小

Ubuntu20.4 bs4安装的正确姿势

一、背景

公司一小伙子反馈在内网机器上通过代理,还是安装不了bs4;于是乎,作为菜鸡的我开始排查。一直认为是网络和代理问题,所以关注点一直放在网络和安装包上;在网上搜索到,主要是以下问题:
1)更新apt-get update,再安装;
2)pip的代理有问题,一直再排查代理
3)是安装bs4,不是beautifulsoup4
 
而自己一直在纠结apt-get源的升级,怎么升都升不上;换了各种源/etc/apt/source.list,也没用。最后觉得可能自己对bs4这个东西不熟悉,还是好好的来了解学习一下,再来解决这个问题;于是乎找到了它的官网。em....;好家伙,一打开就看到了在最新版的Ubuntu下如何安装bs4。整个时间直到解决这个问题前前后后加起来用了一天。
 

二、总结

1)有问题先查官网
2)别人的答案不一定适用于你的问题,比如我的版本是Ubuntu20,而网上的答案都是针对与Ubuntu18的
3)别人给你反馈问题的时候,别人给你的只是他对这个问题的猜想,你得要他提供事实,然后根据自己的判断解决问题;比如这里别人跟我反馈是代理和网络的问题,我就一直往这个方向上去解决,结果完全不是。
 

三、报错信息

root@xxxx:~# pip install bs4
DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. pip 21.0 will drop support for Python 2.7 in January 2021. More details about Python 2 support in pip can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support pip 21.0 will remove support for this functionality.
WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ProxyError('Cannot connect to proxy.', error('Tunnel connection failed: 403 Forbidden',))': /simple/bs4/
WARNING: Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ProxyError('Cannot connect to proxy.', error('Tunnel connection failed: 403 Forbidden',))': /simple/bs4/
WARNING: Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ProxyError('Cannot connect to proxy.', error('Tunnel connection failed: 403 Forbidden',))': /simple/bs4/
WARNING: Retrying (Retry(total=1, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ProxyError('Cannot connect to proxy.', error('Tunnel connection failed: 403 Forbidden',))': /simple/bs4/
WARNING: Retrying (Retry(total=0, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ProxyError('Cannot connect to proxy.', error('Tunnel connection failed: 403 Forbidden',))': /simple/bs4/
ERROR: Could not find a version that satisfies the requirement bs4 (from versions: none)
ERROR: No matching distribution found for bs4

 

四、正确的方式 

#Python2安装
apt-get install -y python-bs4
 
#Python3安装
apt-get install -y python3-bs4

 

五、测试

 
1)输入一段文档
html_doc = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title"><b>The Dormouse's story</b></p>
 
<p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>
 
<p class="story">...</p>
"""

 

2)打印
from bs4 import BeautifulSoup
soup = BeautifulSoup(html_doc, 'html.parser')
 
print(soup.prettify())

 

会输出一段格式好的段落
 

六、apt-get 安装与pip安装的区别

pip:用于直接从PyPI下载和安装软件包。PyPI由Python Software Foundation托管。这是一个专门的软件包管理器,仅处理python软件包。
apt-get:用于从Canonical托管的Ubuntu存储库中下载和安装软件包。用来安装软件,更新源;只会安装最新版本的包,是系统级别的包。
 

七、apt-get相关命令

apt-cache search package   #搜索包
apt-cache show package #获取包的相关信息,如说明、大小、版本等
apt-get install package #安装包
apt-get install package --reinstall #重新安装包
apt-get -f install #强制安装
apt-get remove package #删除包
apt-get remove package - - purge #删除包,包括删除配置文件等
apt-get autoremove #自动删除不需要的包
apt-get update #更新源
apt-get upgrade #更新已安装的包
apt-get dist-upgrade #升级系统
apt-get dselect-upgrade #使用 dselect 升级
apt-cache depends package #了解使用依赖
apt-cache rdepends package #了解某个具体的依赖
apt-get build-dep package #安装相关的编译环境
apt-get source package #下载该包的源代码
apt-get clean && sudo apt-get autoclean #清理下载文件的存档
apt-get check #检查是否有损坏的依赖

 

官网:https://beautifulsoup.readthedocs.io/zh_CN/v4.4.0/
 

八、写在最后

李先生(Lemon),高级运维工程师(自称),SRE专家(目标)。喜欢钻研底层技术,认为底层基础才是王道。一切新技术都离不开操作系统(CPU、内存、磁盘)、网络等。坚持输入输出,记录自己学习的点滴,在平凡中坚持前行,总有一天会遇见不一样的自己。

 

posted on 2021-07-19 22:23  Captain_Li  阅读(549)  评论(0编辑  收藏  举报

导航