环境

Qemu:2.8.0
开发板:vexpress-ca9
 

概述

上一篇博文已经可以让我们的开发板可以成功的ping通百度了,据说Python的网络功能也很强大,而Beautiful Soup是python的一个库,但不是标准库,因此需要单独安装,最主要的功能是从网页抓取数据。
 

正文

一、先用python自带的urllib库试一试
net.py3: 这个是python3版本的
 #!/usr/bin/env python3

 from urllib.request import urlopen
html = urlopen("http://www.pythonscraping.com/pages/page1.html");
print(html.read())
net.py2:这个是python2版本的
 #!/usr/bin/env python2

 from urllib2 import urlopen
html = urlopen("http://www.pythonscraping.com/pages/page1.html");
print(html.read())
我们运行看看结果:
[root@vexpress ~]# ./net.py3
b'<html>\n<head>\n<title>A Useful Page</title>\n</head>\n<body>\n<h1>An Interesting Title</h1>\n<div>\nLorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.\n</div>\n</body>\n</html>\n'
[root@vexpress ~]#
[root@vexpress ~]# ./net.py2
<html>
<head>
<title>A Useful Page</title>
</head>
<body>
<h1>An Interesting Title</h1>
<div>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</div>
</body>
</html>

其实Python提供了一个工具2to3,将Python2版本的代码转换为Python3版本, 我们在板子上面试试。

但是运行后提示找不到2to3:
[root@vexpress ~]# 2to3 net.py2
-/bin/sh: 2to3: not found
但是用which命令查找这个工具发现2to3确实存在
[root@vexpress ~]# which 2to3
/usr/bin/2to3
我们打开/usr/bin/2to3,看到问题所在:
 #!/home/pengdonglin/src/qemu/python_cross_compile/Python2/aarch32/bin/python2.7

 import sys
from lib2to3.main import main
sys.exit(main("lib2to3.fixes"))

问题出在第一行, 修改如下:

 #!/usr/bin/env python2

 import sys
from lib2to3.main import main
sys.exit(main("lib2to3.fixes"))
然后再次运行:
 [root@vexpress ~]# 2to3 net.py2
RefactoringTool: Skipping optional fixer: buffer
RefactoringTool: Skipping optional fixer: idioms
RefactoringTool: Skipping optional fixer: set_literal
RefactoringTool: Skipping optional fixer: ws_comma
RefactoringTool: Refactored net.py2
--- net.py2 (original)
+++ net.py2 (refactored)
@@ -, +, @@
#!/usr/bin/env python2 -from urllib2 import urlopen
+from urllib.request import urlopen html = urlopen("http://www.pythonscraping.com/pages/page1.html"); -print(html.read())
+print((html.read()))
RefactoringTool: Files that need to be modified:
RefactoringTool: net.py2

可以看到以+开始的行就是对应Python3版本的,使用下面的命令会将自动将转换后的文件存储下来:

 [root@vexpress ~]# 2to3 net.py2 -w -n -o /tmp/
lib2to3.main: Output in '/tmp/' will mirror the input directory '' layout.
RefactoringTool: Skipping optional fixer: buffer
RefactoringTool: Skipping optional fixer: idioms
RefactoringTool: Skipping optional fixer: set_literal
RefactoringTool: Skipping optional fixer: ws_comma
RefactoringTool: Refactored net.py2
--- net.py2 (original)
+++ net.py2 (refactored)
@@ -, +, @@
#!/usr/bin/env python2 -from urllib2 import urlopen
+from urllib.request import urlopen html = urlopen("http://www.pythonscraping.com/pages/page1.html"); RefactoringTool: Writing converted net.py2 to /tmp/net.py2.
RefactoringTool: Files that were modified:
RefactoringTool: net.py2

可以看到/tmp/net.py2对应的就是Python3版本的:

 [root@vexpress ~]# cat /tmp/net.py2 

 #!/usr/bin/env python2
from urllib.request import urlopen
html = urlopen("http://www.pythonscraping.com/pages/page1.html");
print((html.read()))
当然,第一行还需要我们手动修改。
 
二、添加BeautifulSoup4模块
由于这个模块是纯Python实现的,所以可以先在PC上面安装这个模块,然后再拷贝到板子上面,毕竟Python代码跟具体的平台无关。
1、为PC安装BeautifulSoup4
 sudo apt-get install python-pip
sudo apt-get install python3-pip
sudo apt-get install python-bs4
sudo pip install beautifulsoup4
sudo pip3 install beautifulsoup4
这样就会在对应版本Python的dist-packages下面看到bs4的目录
 $ls /usr/lib/python2./dist-packages/bs4
builder/ dammit.py dammit.pyc diagnose.py diagnose.pyc element.py element.pyc __init__.py __init__.pyc testing.py testing.pyc tests/
$ls /usr/lib/python3/dist-packages/bs4/
builder/ dammit.py diagnose.py element.py __init__.py __pycache__/ testing.py tests/
有时也会安装到site-packages下面, 然后将这两个bs4文件夹拷贝到共享目录下:
 $cp /usr/lib/python2./dist-packages/bs4 /nfsroot/bs4_python2 -raf
$cp /usr/lib/python3/dist-packages/bs4 /nfsroot/bs4_python3 -raf
如果遇到问题,也可以采用源码安装的方式, 可以到
下载最新的BeautifulSoup4版本, 我下载的是https://www.crummy.com/software/BeautifulSoup/bs4/download/4.5/beautifulsoup4-4.5.3.tar.gz,然后解压缩:
 $tar -xf beautifulsoup4-4.5..tar.gz
$ls beautifulsoup4-4.5.
AUTHORS.txt beautifulsoup4.egg-info/ bs4/ convert-py3k* COPYING.txt doc/ doc.zh/ MANIFEST.in NEWS.txt PKG-INFO README.txt scripts/ setup.cfg setup.py test-all-versions* TODO.txt
在顶层目录下的bs4是用于Python2的,然后通过工具convert-py3k可以生成Python3版本的:
cd beautifulsoup4-4.5./
./convert-py3k

在目录py3k下面的bs4就是用于Python3的,我们可以将这两个bs4分别拷贝到共享目录下:

$cp -raf bs4 /nfsroot/bs4_python2
$cp -raf py3k/bs4 /nfsroot/bs4_python3
同时也应该给PC上面拷贝一份:
sudo cp -raf bs4 /usr/local/lib/python2./site-packages/
sudo cp -raf py3k/bs4 /usr/local/lib/python3./site-packages/
2、然后将对应版本bs4放到板子上面
 [root@vexpress ~]# mount -t nfs -o nolock 192.168.1.100:/nfsroot /mnt
[root@vexpress ~]# cp -raf /mnt/bs4_python2 /usr/lib/python2./site-packages/bs4
[root@vexpress ~]# cp -raf /mnt/bs4_python3/ /usr/lib/python3./site-packages/bs4
验证有没有问题, 执行import bs4:
 [root@vexpress ~]# python2
Python 2.7. (default, Mar , ::)
[GCC 4.8. (prerelease)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import bs4
>>>
[root@vexpress ~]# python3
Python 3.6. (default, Mar , ::)
[GCC 4.8. (prerelease)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import bs4
>>>

如果导入的时候没有报错,表示一切正常。

3、编写测试程序
bs4.py3: Python3版本
 #!/usr/bin/env python3

 from urllib.request import urlopen
from bs4 import BeautifulSoup
html = urlopen("http://www.pythonscraping.com/pages/page1.html")
bsObj = BeautifulSoup(html.read(), "html.parser")
print(bsObj.h1)
bs4.py2:Python2版本
 #!/usr/bin/env python2

 from urllib2 import urlopen
from bs4 import BeautifulSoup
html = urlopen("http://www.pythonscraping.com/pages/page1.html")
bsObj = BeautifulSoup(html.read(), "html.parser")
print(bsObj.h1)
运行:
 [root@vexpress ~]# ./bs4.py3
<h1>An Interesting Title</h1>
[root@vexpress ~]# ./bs4.py2
<h1>An Interesting Title</h1>
 
完。

为Qemu aarch32添加BeautifulSoup4模块的更多相关文章

  1. python---requests和beautifulsoup4模块的使用

    Requests:是使用 Apache2 Licensed 许可证的 基于Python开发的HTTP 库,其在Python内置模块的基础上进行了高度的封装,从而使得Pythoner进行网络请求时,变得 ...

  2. nginx添加 nginx_heath模块

    原因?为什么会使用nginx_heath 这个模块,主要是如nginx+tomcat部署的时,tomcat挂了之后nginx->upstream 轮询是可以踢掉挂掉的tomcat服务的,如果部署 ...

  3. 使用pip安装BeautifulSoup4模块

    1.测试是否安装了BeautifulSoup4模块 import bs4 print bs4 执行报错说明没有安装该模块 Traceback (most recent call last): File ...

  4. nginx 添加nginx-http-concat模块

    github地址:https://github.com/alibaba/nginx-http-concat/tree/master 简单的描述一下吧,网上说的安装新的模块需要重新编译nginx,具体的 ...

  5. 嵌入式linux驱动开发之给linux系统添加温度传感器模块

    忙了几天,终于可以让ds18b20在自己的开发板的linux系统上跑了!虽然ds18b20不是什么新鲜玩意,但是想想知己可以给linux系统添加模块了还是有点小鸡冻呢! 虽然说现在硬件的资源非常丰富而 ...

  6. 动态编译添加php模块

    注意:转载请注明出处:http://www.programfish.com/blog/?p=85 在很多时候我们用linux里搭建web服务器的时候会需要编译安装php套件,而在编译安装后可能又会需要 ...

  7. httpd添加新模块

    */ .hljs { display: block; overflow-x: auto; padding: 0.5em; color: #333; background: #f8f8f8; } .hl ...

  8. yum安装的Nginx添加第三方模块支持tcp

    需求:生产有个接口是通过socket通信.nginx1.9开始支持tcp层的转发,通过stream实现的,而socket也是基于tcp通信. 实现方法:Centos7.2下yum直接安装的nginx, ...

  9. nginx 番外----添加第三方模块

    #第三方模块需要先进行下载,然后再编译时指定文件目录 1.查看当前编译模块 root@nginx sbin]# ./nginx -V #查看当前添加模块 nginx version: nginx/ b ...

随机推荐

  1. MySQL管理工具MySQL Utilities — 介绍与安装(1)

    MySQL Utilities介绍 MySQL Utilities 提供一组命令行工具用于维护和管理 MySQL 服务器,包括: 管理工具 (克隆.复制.比较.差异.导出.导入) 复制工具 (安装.配 ...

  2. OpenWRT开发之——对C++的支持(解决库依赖问题)【转】

    转自:https://my.oschina.net/hevakelcj/blog/411944 摘要: 本文尝试用C++来开发一个cpp-demo包 遇到打包库依赖的问题,分析打包过程并解决了这个问题 ...

  3. 促使团队紧密协作[高效能程序员的修炼-N1]

    在Jeff看来,团队里最重要的事情,是人与人之间地协作和沟通!所有的问题,其实都是人的问题.“不管什么问题,那总是人的问题”-温伯格.即,让你和团队陷入困境的最快的方法,就是认为技术是决定性的因素,而 ...

  4. WEBAPI 帖子收藏

    [翻译]ASP.NET Web API入门 [翻译]ASP.NET WEB API异常处理 ASP.NET WebAPI 路由规则与POST数据 ASP.NET Web API路由规则(二) ASP. ...

  5. python3操作sqlserver,查询数据统计导出csv

    import pymssql #导入sqlserver连接池模块 import csv #导出csv文件使用模块 conn=pymssql.connect('服务器ip','用户名','密码','数据 ...

  6. mac OS配置用户全局环境变量(设置字符集为UTF8)

    mac OS系统跟linux系统一样也是将用户的全局环境变量保存在.bash_profile配置文件中,只是mac OS默认没有此文件. 1.创建.bash_profile文件 vi ~/.bash_ ...

  7. 7z

    7zip是一款开源的解压缩软件,不仅自己独有的7z格式,而且支持zip,rar,tar,gzip等众多其他格式,同时7z格式的压缩比例很高,目前很多硬盘版的游戏都采用zip进行打包.下面介绍一下Lin ...

  8. linux 命令点滴记录(centos)

    2016年5月26日:创建root用户 [lx@localhost ~]$ su root Password: [root@localhost lx]# 帐号:root ;密码:输入的Password ...

  9. spfa学习笔记

    序 spfa它死了 --by 大佬 但是本蒟蒻还是一如既往的使用spfa... 因为太弱了,其他什么都不会.于是就疯狂开O2跪倒在spfa上. 例题--汽车加油行驶问题 loj跳转链接 luogu跳转 ...

  10. 【PAT】1019 数字黑洞 (20)(20 分)

    1019 数字黑洞 (20)(20 分) 给定任一个各位数字不完全相同的4位正整数,如果我们先把4个数字按非递增排序,再按非递减排序,然后用第1个数字减第2个数字,将得到一个新的数字.一直重复这样做, ...