BeautifulSoup 安装使用
Linux环境
1. 安装
方法一:
下载:http://www.crummy.com/software/BeautifulSoup/bs4/download/4.2/
解压:tar -xzvf beautifulsoup4-4.2.0.tar.gz
安装:进入解压后的目录
python setup.py build
sudo python setup.py install
方法二(快速安装)
(Ubuntu) sudo apt-get install python-bs4
或者
install beautifulsoup4
或着
easy_install beautifulsoup4
2. 引用(python环境下)
from bs4 import BeautifulSoup
3. 使用
案例
html_doc = """
<html><head><title>The Dormouse's story</title></head> <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="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p> <p class="story">...</p>
"""
开始
from bs4 import BeautifulSoup
soup = BeautifulSoup(html_doc)
>>> soup.head()
[<title>The Dormouse's story</title>]
>>> soup.title
<title>The Dormouse's story</title>
>>> soup.title.string
u"The Dormouse's story"
>>> soup.body.b
<b>The Dormouse's story</b>
>>> soup.body.b.string
u"The Dormouse's story"
>>> soup.a
<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>
找到所有的a
soup.find_all('a')
[<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>, <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>, <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]
打印每个a中的信息
>>> for key in soup.find_all('a'):
... print key.get('class'), key.get("href")
...
['sister'] http://example.com/elsie
['sister'] http://example.com/lacie
['sister'] http://example.com/tillie
参考
http://www.crummy.com/software/BeautifulSoup/bs4/doc/
BeautifulSoup 安装使用的更多相关文章
- Python - BeautifulSoup 安装
BeautifulSoup 3.x 1. 下载 BeautifulSoup. [huey@huey-K42JE python]$ wget http://www.crummy.com/software ...
- beautifulSoup安装
Python2.7 + beautifulSoup 4.4.1 安装配置 原创 2016年05月09日 10:20:30 标签: python 1261 1. 前言 最近研究python 的爬虫功能, ...
- 【Python实例二】之前期准备:Windows下的BeautifulSoup安装
前言 一直久闻Python的爬虫很高效,而且操作便捷,因此决定开始练习爬虫的相关内容. 首先尝试的是Python的爬虫利器之一:BeautifulSoup.(这名字听起来就有种想要去探究的兴趣.... ...
- [Python]BeautifulSoup安装与使用
1.BeautifulSoup简介 BeautifulSoup4和 lxml 一样,Beautiful Soup 也是一个HTML/XML的解析器,主要的功能也是如何解析和提取 HTML/XML 数据 ...
- beautifulsoup 安装
pip install beautifulsoup4
- Python网络爬虫与信息提取(二)—— BeautifulSoup
BeautifulSoup官方介绍: Beautiful Soup 是一个可以从HTML或XML文件中提取数据的Python库.它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档的方式. 官方 ...
- 解析库-beautifulsoup模块
# -*- coding: utf-8 -*- from bs4 import BeautifulSoup # 安装:pip install beautifulsoup4 # Beautiful So ...
- Python HTML解析器BeautifulSoup(爬虫解析器)
BeautifulSoup简介 我们知道,Python拥有出色的内置HTML解析器模块——HTMLParser,然而还有一个功能更为强大的HTML或XML解析工具——BeautifulSoup(美味的 ...
- python学习(25) BeautifulSoup介绍和实战
BeautifulSoup是python的html解析库,处理html非常方便 BeautifulSoup 安装 pip install beautifulsoup4 BeautifulSoup 配合 ...
随机推荐
- Xamarin.Android之山有木兮之木有枝,心悦君兮君不知。
Xamarin.Android之山有木兮之木有枝,心悦君兮君不知. 第一步,写项目中的第一个界面. <?xml version="1.0" encoding =" ...
- SwipeBackLayout的使用方法,右滑返回
使用方法: 需要右滑返回的activity继承baseActivity就可以, 如: public class SettingActivity extends BaseActivity {} 为防止滑 ...
- nginx简单反向代理和负载均衡(ubuntu)
nginx简单反向代理与负载均衡 环境:三台ubuntu 12.04.5 虚拟机 均装有nginx 1.1.19 以下u1(192.168.240.129) ,u2(192.168.240.13 ...
- 设计模式之组合模式(Composite)
组合模式原理:组合模式的作用是讲继承同一父类的不同子类对象组合起来,形成一个树形的结构,例如公司的部门组织 代码如下 #include <iostream> #include <st ...
- 【BZOJ】【1023】【SHOI2008】cactus仙人掌图
DP+单调队列/仙人掌 题解:http://hzwer.com/4645.html->http://z55250825.blog.163.com/blog/static/150230809201 ...
- NYOJ-975 关于521 AC 分类: NYOJ 2014-02-25 22:14 349人阅读 评论(0) 收藏
#include<stdio.h> struct AC { int x,y; }a[1000004]; int main() { int i,j,k=0;a[125].x=1,a[521] ...
- 引擎设计跟踪(九.14.2c) 最近一些小的更新
1. bump map与normal map 昨天拿了crytek sponza(http://www.crytek.com/cryengine/cryengine3/downloads)场景测试, ...
- StoreKit framework
关于In-APP Purchase 1. 中文文档 基本流程: http://www.cnblogs.com/wudan7/p/3621763.html 三种购买形式及StoreKit code介绍: ...
- UICollectionViewLayout
http://blog.csdn.net/majiakun1/article/details/17204921
- javascript document.write
在载人页面后,浏览器输出流自动关闭:在此之后,任何一个对当前页面进行操作的document.write()方法将打开—个新的输出流.它将清除当前页面内容(包括源文档的任何变量或值).document. ...