pythn BeautifulSoup
http://rsj217.diandian.com/post/2012-11-01/40041235132
Beautiful Soup 是用 Python 写的一个 HTML/XML 的解析器,它可以很好的处理不规范标记并生成剖析树。通常用来分析爬虫抓取的web文档。对于 不规则的 Html文档,也有很多的补全功能,节省了开发者的时间和精力。
Beautiful Soup 的官方文档齐全,将官方给出的例子实践一遍就能掌握。官方英文文档,中文文档
一 安装 Beautiful Soup
安装 BeautifulSoup 很简单,下载 BeautifulSoup 源码。解压运行
python setup.py install 即可。
测试安装是否成功。键入 import BeautifulSoup 如果没有异常,即成功安装
二 使用 BeautifulSoup
1. 导入BeautifulSoup ,创建BeautifulSoup 对象
|
1
2
3
4
5
6
7
8
9
10
11
12
|
from BeautifulSoup import BeautifulSoup # HTMLfrom BeautifulSoup import BeautifulStoneSoup # XMLimport BeautifulSoup # ALL doc = [ '<html><head><title>Page title</title></head>', '<body><p id="firstpara" align="center">This is paragraph <b>one</b>.', '<p id="secondpara" align="blah">This is paragraph <b>two</b>.', '</html>']# BeautifulSoup 接受一个字符串参数soup = BeautifulSoup(''.join(doc)) |
2. BeautifulSoup对象简介
用BeautifulSoup 解析 html文档时,BeautifulSoup将 html文档类似 dom文档树一样处理。BeautifulSoup文档树有三种基本对象。
2.1. soup BeautifulSoup.BeautifulSoup
|
1
2
|
type(soup)<class 'BeautifulSoup.BeautifulSoup'> |
2.2. 标记 BeautifulSoup.Tag
|
1
2
|
type(soup.html)<class 'BeautifulSoup.Tag'> |
2.3 文本 BeautifulSoup.NavigableString
|
1
2
|
type(soup.title.string)<class 'BeautifulSoup.NavigableString'> |
3. BeautifulSoup 剖析树
3.1 BeautifulSoup.Tag对象方法
获取 标记对象(Tag)
标记名获取法 ,直接用 soup对象加标记名,返回 tag对象.这种方式,选取唯一标签的时候比较有用。或者根据树的结构去选取,一层层的选择
|
1
2
3
4
5
6
7
|
>>> html = soup.html>>> html<html><head><title>Page title</title></head><body><p id="firstpara" align="center">This is paragraph<b>one</b>.</p><p id="secondpara" align="blah">This is paragraph<b>two</b>.</p></body></html>>>> type(html)<class 'BeautifulSoup.Tag'>>>> title = soup.title<title>Page title</title> |
content方法
content方法 根据文档树进行搜索,返回标记对象(tag)的列表
|
1
2
|
>>> soup.contents[<html><head><title>Page title</title></head><body><p id="firstpara" align="center">This is paragraph<b>one</b>.</p><p id="secondpara" align="blah">This is paragraph<b>two</b>.</p></body></html>] |
|
1
2
3
4
5
6
|
>>> soup.contents[0].contents[<head><title>Page title</title></head>, <body><p id="firstpara" align="center">This is paragraph<b>one</b>.</p><p id="secondpara" align="blah">This is paragraph<b>two</b>.</p></body>]>>> len(soup.contents[0].contents)2>>> type(soup.contents[0].contents[1])<class 'BeautifulSoup.Tag'> |
使用contents向后遍历树,使用parent向前遍历树
next 方法
获取树的子代元素,包括 Tag 对象 和 NavigableString 对象。。。
|
1
2
3
4
|
>>> head.next<title>Page title</title>>>> head.next.nextu'Page title' |
|
1
2
3
4
5
|
>>> p1 = soup.p>>> p1<p id="firstpara" align="center">This is paragraph<b>one</b>.</p>>>> p1.nextu'This is paragraph' |
nextSibling 下一个兄弟对象 包括 Tag 对象 和 NavigableString 对象
|
1
2
3
4
|
>>> head.nextSibling<body><p id="firstpara" align="center">This is paragraph<b>one</b>.</p><p id="secondpara" align="blah">This is paragraph<b>two</b>.</p></body>>>> p1.next.nextSibling<b>one</b> |
与 nextSibling 相似的是 previousSibling,即上一个兄弟节点。
replacewith方法
将对象替换为,接受字符串参数
|
1
2
3
4
5
6
7
8
9
10
11
12
|
>>> head = soup.head>>> head<head><title>Page title</title></head>>>> head.parent<html><head><title>Page title</title></head><body><p id="firstpara" align="center">This is paragraph<b>one</b>.</p><p id="secondpara" align="blah">This is paragraph<b>two</b>.</p></body></html>>>> head.replaceWith('head was replace')>>> head<head><title>Page title</title></head>>>> head.parent>>> soup<html>head was replace<body><p id="firstpara" align="center">This is paragraph<b>one</b>.</p><p id="secondpara" align="blah">This is paragraph<b>two</b>.</p></body></html>>>> |
搜索方法
搜索提供了两个方法,一个是 find,一个是findAll。这里的两个方法(findAll和 find)仅对Tag对象以及,顶层剖析对象有效,但 NavigableString不可用。
findAll(name, attrs, recursive, text, limit, **kwargs)
接受一个参数,标记名
寻找文档所有 P标记,返回一个列表
|
1
2
3
4
|
>>> soup.findAll('p')[<p id="firstpara" align="center">This is paragraph<b>one</b>.</p>, <p id="secondpara" align="blah">This is paragraph<b>two</b>.</p>]>>> type(soup.findAll('p'))<type 'list'> |
寻找 id="secondpara"的 p 标记,返回一个结果集
|
1
2
3
|
>>> pid = type(soup.findAll('p',id='firstpara'))>>> pid<class 'BeautifulSoup.ResultSet'> |
传一个属性或多个属性对
|
1
2
3
4
5
|
>>> p2 = soup.findAll('p',{'align':'blah'})>>> p2[<p id="secondpara" align="blah">This is paragraph<b>two</b>.</p>]>>> type(p2)<class 'BeautifulSoup.ResultSet'> |
利用正则表达式
|
1
2
|
>>> soup.findAll(id=re.compile("para$"))[<p id="firstpara" align="center">This is paragraph<b>one</b>.</p>, <p id="secondpara" align="blah">This is paragraph<b>two</b>.</p>] |
读取和修改属性
|
1
2
3
4
5
6
7
8
9
10
11
12
|
>>> p1 = soup.p>>> p1<p id="firstpara" align="center">This is paragraph<b>one</b>.</p>>>> p1['id']u'firstpara'>>> p1['id'] = 'changeid'>>> p1<p id="changeid" align="center">This is paragraph<b>one</b>.</p>>>> p1['class'] = 'new class'>>> p1<p id="changeid" align="center" class="new class">This is paragraph<b>one</b>.</p>>>> |
剖析树基本方法就这些,还有其他一些,以及如何配合正则表达式。具体请看官方文档
3.2 BeautifulSoup.NavigableString对象方法
NavigableString 对象方法比较简单,获取其内容
|
1
2
3
4
5
6
7
8
9
|
>>> soup.title<title>Page title</title>>>> title = soup.title.next>>> titleu'Page title'>>> type(title)<class 'BeautifulSoup.NavigableString'>>>> title.stringu'Page title' |
至于如何遍历树,进而分析文档,已经 XML 文档的分析方法,可以参考官方文档。
pythn BeautifulSoup的更多相关文章
- Python爬虫小白入门(三)BeautifulSoup库
# 一.前言 *** 上一篇演示了如何使用requests模块向网站发送http请求,获取到网页的HTML数据.这篇来演示如何使用BeautifulSoup模块来从HTML文本中提取我们想要的数据. ...
- 使用beautifulsoup与requests爬取数据
1.安装需要的库 bs4 beautifulSoup requests lxml如果使用mongodb存取数据,安装一下pymongo插件 2.常见问题 1> lxml安装问题 如果遇到lxm ...
- BeautifulSoup :功能使用
# -*- coding: utf-8 -*- ''' # Author : Solomon Xie # Usage : 测试BeautifulSoup一些用法及容易出bug的地方 # Envirom ...
- BeautifulSoup研究一
BeautifulSoup的文档见 https://www.crummy.com/software/BeautifulSoup/bs4/doc.zh/ 其中.contents 会将换行也记录为一个子节 ...
- BeautifulSoup
参考:http://www.freebuf.com/news/special/96763.html 相关资料:http://www.jb51.net/article/65287.htm 1.Pytho ...
- BeautifulSoup Some characters could not be decoded, and were replaced with REPLACEMENT CHARACTER.
BeautifulSoup很赞的东西 最近出现一个问题:Python 3.3 soup=BeautifulSoup(urllib.request.urlopen(url_path),"htm ...
- beautifulSoup(1)
import re from bs4 import BeautifulSoupdoc = ['<html><head><title>Page title</t ...
- python BeautifulSoup模块的简要介绍
常用介绍: pip install beautifulsoup4 # 安装模块 from bs4 import BeautifulSoup # 导入模块 soup = BeautifulSoup(ht ...
- BeautifulSoup 的用法
转自:http://cuiqingcai.com/1319.html Beautiful Soup支持Python标准库中的HTML解析器,还支持一些第三方的解析器,如果我们不安装它,则 Python ...
随机推荐
- Linux环境下Websphere重启
一.Websphere控制台重启 1.更新class文件发布,Websphere自动重启. 2.更新web.xml发布,需要手动更新web.xml或者更新项目. web.config 缓存位置: We ...
- 第六十篇、音视频采集硬编码(H264+ACC)
使用 AVCaptureSession进行实时采集音视频(YUV.),编码 通过AVCaptureVideoDataOutputSampleBufferDelegate获取到音视频buffer- 数据 ...
- HTML+CSS学习笔记(3)- 认识标签(2)
HTML+CSS学习笔记(3)- 认识标签(2) 1.使用ul,添加新闻信息列表 在浏览网页时,你会发现网页上有很多信息的列表,如新闻列表.图片列表, 这些列表就可以使用ul-li标签来完成.ul-l ...
- GitHub 基本操作流程
GitHub是最先进的分布式版本控制工具,下面是我学习中总结的操作流程,仅供参考 ----------------------------------------------------------- ...
- log4j定义某个类的日志级别
项目引入了定时任务后,当我把已有的定时任务删除后,控制台一直会打出类似于 [org.springframework.scheduling.quartz.LocalDataSourceJobStore] ...
- 《APUE》第五章练习1
题目:用setvbuf实现setbuf. 这两个函数都是改变流的缓冲模式的.函数原型如下: #include <stdio.h> void setbuf(FILE *fp, char *b ...
- Java 学习计划
第一部分 在搭建SSM的过程中,可能会经常接触到一个叫maven的工具.这个工具也是你以后工作当中几乎是必须要使用的工具,所以你在搭建SSM的过程中,也可以顺便了解一下maven的知识.在你目前这个阶 ...
- winform 与 html 交互 简单案例
本文主要简单的记录winform如何与html文件中的信息如何进行交互,即在winform中加载html界面,从而可以进行相互调用. 1.新建一个winform项目,若要在winform中加载html ...
- 斐波那契数 c 语言实现
斐波那契数列,又称黄金数列,指的是这样一个数列:1.1.2.3.5.8.13.21.……在数学上,斐波纳契数列以如下被以递归的方法定义:F(1)=1,F(2)=1,F(n)=F(n-1)+F(n-2) ...
- 解决ListView滑动时卡的问题,实现异步加载图片解决
ListView是最为常见的空间之一,现在的应用的呈现形式大多数都需要用到ListView来呈现,以列表的方式最直观最便于操作. 那么在使用的过程中大家一定使用adapter适配器来匹配这个ListV ...