树莓派上安装python3.6以及lxml, BeautifulSoup4包遇到的问题
树莓派的低功耗、适合长时间运行的特性,很适合作为爬虫运行的平台,家里闲置了一台树莓派3,这次打算安装配置python3.6和相关依赖包来搭建一个爬虫环境。
需要安装的python版本和依赖包如下:
- Python 3.6.2
- lxml
- BeautifulSoup4
安装Python3.6
从源码安装Python3的过程就不赘述,基本上是下面几个步骤
1 2 3 4 5 |
[crayon-60079f71c8d21759468544 inline="true" ]# wget https://www.python.org/ftp/python/3.6.2/Python-3.6.2.tgz # tar xvf Python-3.6.2.tgz # cd Python-3.6.2 # ./configure # make && make install |
[/crayon]
pip命令遇到ssl module in Python is not available的错误
如果用pip命令安装所依赖的 lxml和 BeautifulSoup4包,会遇到下面这个奇怪的错误.
1 2 |
[crayon-60079f71c8d2f976804352 inline="true" ]# /usr/local/bin/pip3.6 install lxml pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available. |
[/crayon]
1. 在网上搜了一圈,原因是树莓派上运行的基于Deiban的Raspbian系统里,默认没有安装openssl包,运行下面一条命令装上。
1 |
[crayon-60079f71c8d37196242779 inline="true" ]# apt-get install openssl openssl-dev |
[/crayon]
2. 编译Python需要加入ssl的模块,打开Modules/Setup,修改204行左右成下面这个样子.
1 2 3 4 5 6 7 8 9 |
[crayon-60079f71c8d3d503451339 inline="true" ]# Socket module helper for socket(2) _socket socketmodule.c # Socket module helper for SSL support; you must comment out the other # socket line above, and possibly edit the SSL variable: #SSL=/usr/local/ssl _ssl _ssl.c \ -DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \ -L$(SSL)/lib -lssl -lcrypto |
[/crayon]
3. 重新编译安装
1 |
[crayon-60079f71c8d44048712695 inline="true" ]# make && make install |
[/crayon]
pip安装lxml遇到的错误
首先遇到Could not find function xmlCheckVersion in library libxml2的错误,很明显,libxml2的软件包没有安装,运行下面一条命令搞定.
1 |
[crayon-60079f71c8d4b968726736 inline="true" ]# apt-get install libxml2 libxml2-dev |
[/crayon]
再次安装又遇到另外一条错误
src/lxml/includes/etree_defs.h:14:31: fatal error: libxml/xmlversion.h: No such file or directory
运行这条命令解决
1 |
[crayon-60079f71c8d52929837048 inline="true" ]# export C_INCLUDE_PATH=/usr/include/libxml2/ |
[/crayon]
再次安装lxml,又遇到一个错误
src/lxml/includes/etree_defs.h:23:32: fatal error: libxslt/xsltconfig.h: No such file or directory
看来是缺少 libxslt软件包,安装上去
1 |
[crayon-60079f71c8d5c074476883 inline="true" ]apt-get install libxslt1-dev |
[/crayon]
再次安装lxml成功完成,没有产生其他错误。
安装BeautifulSoup4
BeautifulSoup4安装起来比较顺利,没有报任何错误。
1 |
[crayon-60079f71c8d67772831425 inline="true" ]# /usr/local/bin/pip3 install bs4 |
[/crayon]
验证安装完成
最后进入Python解释器验证一下两个包是否正确运行,一切OK,基本的爬虫环境搭建完成。
1 2 |
[crayon-60079f71c8d6e372685388 inline="true" ]import lxml from bs4 import BeautifulSoup |
[/crayon]
本文出自扉启博客,转载时请注明出处及相应链接。
本文永久链接: https://www.feiqy.com/install-python3-lxml-beautifulsoup-on-raspberry-pi/
近期评论