python3 安裝 Beautifulsoup4 版本不相容問題
阿新 • • 發佈:2019-02-12
注意我的python的windows安裝目錄為:D:\Python37-32
python進行爬蟲時引用到BeautifuSoup開源的xml解析工具。以下為beautifulsoup4的windows的安裝步驟:
首先去網站下載beautifulsoup壓縮包(beautifulsoup4-4.6.0.tar.gz)
https://www.crummy.com/software/BeautifulSoup/#Download
將下載下來的壓縮包解壓後放入到python的安裝目錄(D:\Python37-32\beautifulsoup4-4.6.0)
進入cmd,執行命令:
cd D:\Python37-32\beautifulsoup4-4.6.0
setup.py build
setup.py install
輸入驗證是否安裝成功:
在命令列輸入:python
然後輸入:from bs4 import BeautifulSoup
如果不出現任何提示資訊表示安裝成功。如果如下所示則安裝失敗,版本不相容
在python3中引用BeautifuSoup4時會出現版本不相容的情況,具體看下面的錯誤程式碼
>>> from bs4 import BeautiulSoup Traceback (most recent call last): File "<stdin>", line 1, in <module> File "D:\Python37-32\beautifulsoup4-4.6.0\bs4\__init__.py", line 53 'You are trying to run the Python 2 version of Beautiful Soup under Python 3. This will not work.'<>'You need to convert the code, either by installing it (`python setup.py install`) or by running 2to3 (`2to3 -w bs4`).' ^ SyntaxError: invalid syntax
將beautifulSoup解壓目錄中的bs4資料夾(D:\Python37-32\beautifulsoup4-4.6.0\bs4)和 2to3.py(D:\Python37-32\Tools\scripts\)複製到python的安裝目錄下的Lib(D:\Python37-32\Lib)資料夾下
進入D:\Python37-32\Lib 目錄,並執行 2to3.py bs4 -w 命令
cd D:\Python37-32\Lib
2to3.py bs4 -w
最後再輸入python
然後輸入 from bs4 import BeautifulSoup