注意我的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,執行指令:
[html] view plain copy
- cd D:\Python37-32\beautifulsoup4-4.6.0
- setup.py build
- setup.py install
輸入驗證是否安裝成功:
在指令行輸入:python
然後輸入:from bs4 import BeautifulSoup
如果不出現任何提示資訊表示安裝成功。如果如下所示則安裝失敗,版本不相容
在python3中引用BeautifuSoup4時會出現版本不相容的情況,具體看下面的錯誤代碼
[python] view plain copy
- >>> 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 指令
[plain] view plain copy
- cd D:\Python37-32\Lib
- 2to3.py bs4 -w
最後再輸入python
然後輸入 from bs4 import BeautifulSoup