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="//example.com/elsie" class="sister" id="link1">Elsie</a>,
<a href="//example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="//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 #檢查是否有損壞的依賴

 

官網://beautifulsoup.readthedocs.io/zh_CN/v4.4.0/
 

八、寫在最後

李先生(Lemon),高級運維工程師(自稱),SRE專家(目標)。喜歡鑽研底層技術,認為底層基礎才是王道。一切新技術都離不開操作系統(CPU、內存、磁盤)、網絡等。堅持輸入輸出,記錄自己學習的點滴,在平凡中堅持前行,總有一天會遇見不一樣的自己。公眾號:運維汪(ID:Leeeee_Li)。

Tags: