python 模块BeautifulSoup使用
BeautifulSoup是一个专门用于解析html/xml的库。官网:http://www.crummy.com/software/BeautifulSoup/
说明,BS有了4.x的版本了。官方说:
Beautiful Soup 3 has been replaced by Beautiful Soup 4. You may be looking for the Beautiful Soup 4 documentation
Beautiful Soup 3 only works on Python 2.x, but Beautiful Soup 4 also works on Python 3.x. Beautiful Soup 4 is faster, has more features, and works with third-party parsers like lxml and html5lib. You should use Beautiful Soup 4 for all new projects.
我的电脑上面用
help(BeautifulSoup.__version__)看到版本号为:
3.2.1
Beautiful Soup 4 works on both Python 2 (2.6+) and Python 3.
安装其实很简单,BeautifulSoup只有一个文件,只要把这个文件拷到你的工作目录,就可以了。
from BeautifulSoup import BeautifulSoup # For processing HTML
from BeautifulSoup import BeautifulStoneSoup # For processing XML
import BeautifulSoup # To get everything
创建 BeautifulSoup 对象
BeautifulSoup对象需要一段html文本就可以创建了。
下面的代码就创建了一个BeautifulSoup对象:
from BeautifulSoup import BeautifulSoup
doc = ['<html><head><title>PythonClub.org</title></head>',
'<body><p id="firstpara" align="center">This is paragraph <b>one</b> of ptyhonclub.org.',
'<p id="secondpara" align="blah">This is paragraph <b>two</b> of pythonclub.org.',
'</html>']
soup = BeautifulSoup(''.join(doc)) 采用
print soup.prettify()
后:
# <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>
查找HTML内指定元素
BeautifulSoup可以直接用”.”访问指定HTML元素
根据html标签(tag)查找:查找html title
可以用 soup.html.head.title 得到title的name,和字符串值。
>>> soup.html.head.title 注意,包含title标签
<title>PythonClub.org</title>
>>> soup.html.head.title.name
u'title'
>>> soup.html.head.title.string
u'PythonClub.org'
>>>
也可以直接通过soup.title直接定位到指定HTML元素:
>>> soup.title
<title>PythonClub.org</title>
>>>
根据html内容查找:查找包含特定字符串的整个标签内容
下面的例子给出了查找含有”para”的html tag内容:
>>> soup.findAll(text=re.compile("para"))
[u'This is paragraph ', u'This is paragraph ']
>>> soup.findAll(text=re.compile("para"))[0].parent
<p id="firstpara" align="center">This is paragraph <b>one</b> of ptyhonclub.org.</p>
>>> soup.findAll(text=re.compile("para"))[0].parent.contents
[u'This is paragraph ', <b>one</b>, u' of ptyhonclub.org.']
基本的方法:findAll
findAll(name=None, attrs={}, recursive=True, text=None, limit=None, **kwargs)
These arguments show up over and over again throughout the Beautiful Soup API. The most important arguments are name and the keyword arguments.
The simplest usage is to just pass in a tag name. This code finds all the <B>
Tags in the document:soup.findAll('b')
#[<b>one</b>, <b>two</b>]You can also pass in a regular expression. This code finds all the tags whose names start with B:
import re
tagsStartingWithB = soup.findAll(re.compile('^b'))
[tag.name for tag in tagsStartingWithB]
#[u'body', u'b', u'b']You can pass in a list or a dictionary. These two calls find all the <TITLE> and all the <P> tags. They work the same way, but the second call runs faster:
soup.findAll(['title', 'p'])
#[<title>Page title</title>,
# <p id="firstpara" align="center">This is paragraph <b>one</b>.</p>,
# <p id="secondpara" align="blah">This is paragraph <b>two</b>.</p>] soup.findAll({'title' : True, 'p' : True})
#[<title>Page title</title>,
# <p id="firstpara" align="center">This is paragraph <b>one</b>.</p>,
# <p id="secondpara" align="blah">This is paragraph <b>two</b>.</p>]
The keyword arguments impose restrictions on the attributes of a tag. This simple example finds all the tags which have a value of "center" for their "align" attribute:
soup.findAll(align="center")
#[<p id="firstpara" align="center">This is paragraph <b>one</b>.</p>]
Searching by CSS class
The attrs argument would be a pretty obscure feature were it not for one thing: CSS. It's very useful to search for a tag that has a certain CSS class, but the name of the CSS attribute, class, is also a Python reserved word.
You could search by CSS class with soup.find("tagName", { "class" : "cssClass" }), but that's a lot of code for such a common operation. Instead, you can pass a string for attrs instead of a dictionary. The string will be used to restrict the CSS class.
from BeautifulSoup import BeautifulSoup
soup = BeautifulSoup("""Bob's <b>Bold</b> Barbeque Sauce now available in
<b class="hickory">Hickory</b> and <b class="lime">Lime</a>""") soup.find("b", { "class" : "lime" })
#<b class="lime">Lime</b> soup.find("b", "hickory")
#<b class="hickory">Hickory</b>
根据CSS属性查找HTML内容
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>]
soup.findAll(attrs={'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>]
深入理解BeautifulSoup
转自:http://www.pythonclub.org/modules/beautifulsoup/start
http://www.crummy.com/software/BeautifulSoup/bs4/doc/
一篇文章
------------------------------------
汤料——Soup中的对象
标签(Tag)
标签对应于HTML元素,也就是应于一对HTML标签以及括起来的内容(包括内层标签和文本),如:
soup = BeautifulSoup('<b class="boldest">Extremely bold</b>')
tag = soup.b
soup.b就是一个标签,soup其实也可以视为是一个标签,其实整个HTML就是由一层套一层的标签组成的。
名字(Name)
名字对应于HTML标签中的名字(也就是尖括号里的第一项)。每个标签都具有名字,标签的名字使用.name来访问,例如上例中,
tag.name == u'b'
soup.name == u'[document]'
属性(Atrriutes)
属性对应于HTML标签中的属性部分(也就是尖括号里带等号的那些)。标签可以有许多属性,也可以没有属性。属性使用类似于字典的形式访问,用方括号加属性名,例如上例中,
tag['class'] == u'boldest'
可以使用.attrs直接获得这个字典,例如,
tag.attrs == {u'class': u'boldest'}
文本(Text)
文本对应于HTML中的文本(也就是尖括号外的部分)。文件使用.text来访问,例如上例中,
tag.text == u'Extremely bold'
string和text区别:
找汤料——Soup中的查找
解析一个HTML通常是为了找到感兴趣的部分,并提取出来。BeautifulSoup提供了find和find_all的方法进行查找。find只返回找到的第一个标签,而find_all则返回一个列表。因为查找用得很多,所以BeautifulSoup做了一些很方便的简化的使用方式:
tag.find_all("a") #等价于 tag("a") 这是4.0的函数find_all
tag.find("a") #等价于 tag.a
因为找不到的话,find_all返回空列表,find返回None,而不会抛出异常,所以,也不用担心 tag("a") 或tag.a 会因为找不到而报错。限于python的语法对变量名的规定,tag.a 的形式只能是按名字查找,因为点号.后面只能接变量名,而带括号的形式 tag() 或 tag.find() 则可用于以下的各种查找方式。
查找可以使用多种方式:字符串、列表、键-值(字典)、正则表达式、函数
字符串: 字符串会匹配标签的名字,例如
tag.a或tag("a")列表: 可以按一个字符串列表查找,返回名字匹配任意一个字符串的标签。例如
tag("h2", "p")键-值: 可以用
tag(key=value)的形式,来按标签的属性查找。键-值查找里有比较多的小花招,这里列几条:- class
class是Python的保留字,不能当变量名用,偏偏在HTML中会有很多class=XXX的情况,BeautifulSoup的解决方法是加一下划线,用class_代替,如tag(class_=XXX)。 - True
当值为True时,会匹配所有带这个键的标签,如tag(href=True) - text
text做为键时表示查找按标签中的文本查找,如tag(text=something)
- class
正则表达式: 例如
tag(href=re.compile("elsie"))函数: 当以上方法都行不通时,函数是终极方法。写一个以单个标签为参数的函数,传入
find或find_all进行查找。如def fun(tag):
return tag.has_key("class") and not tag.has_key("id")
tag(fun) # 会返回所有带class属性但不带id属性的标签
再来一碗——按文档的结构查找
HTML可以解析成一棵标签树,因此也可以按标签在树中的相互关系来查找。
查找上层节点:
find_parents()和find_parent()查找下一个兄弟节点:
find_next_siblings()和find_next_sibling()- 查找上一个兄弟节点:
find_previous_siblings()和find_previous_sibling()
以上四个都只会查同一父节点下的兄弟
查找下层节点:其实上面说的find和find_all就是干这活的
查找下一个节点(无视父子兄弟关系)
find_all_next()和find_next()- 查找上一个节点(无视父子兄弟关系)
find_all_previous()和find_previous()
以上的这些查找的参都和find一样,可以搭配着用。
看颜色选汤——按CSS查找
用 .select()方法,看 http://www.crummy.com/software/BeautifulSoup/bs4/doc/#css-selectors
一些小花招
- BeautifulSoup 可以支持多种解析器,如lxml, html5lib, html.parser. 如:
BeautifulSoup("<a></b>", "html.parser")
具体表现可参考 http://www.crummy.com/software/BeautifulSoup/bs4/doc/#differences-between-parsers
BeautifulSoup 在解析之前会先把文本转换成unicode,可以用
from_encoding指定编码,如:BeautifulSoup(markup, from_encoding="iso-8859-8")soup.prettify()可以输出排列得很好看的HTML文本,遇上中文的话可以指定编码使其显示正常,如
soup.prettify("gbk")还是有编码问题,看:http://www.crummy.com/software/BeautifulSoup/bs4/doc/#unicode-dammit
转自:http://cndenis.iteye.com/blog/1746706
soup2个重要的属性:
.contents and .children
A tag’s children are available in a list called .contents:
head_tag = soup.head
head_tag
# <head><title>The Dormouse's story</title></head> head_tag.contents
[<title>The Dormouse's story</title>]
type(head_tag.contents[0])
<class 'BeautifulSoup.Tag'> 说明content里面的类型不是string,而是固有的类型
title_tag = head_tag.contents[0]
title_tag
# <title>The Dormouse's story</title>
title_tag.contents
# [u'The Dormouse's story']
The BeautifulSoup object itself has children. In this case, the <html> tag is the child of the BeautifulSoup object.:
len(soup.contents)
# 1
soup.contents[0].name
# u'html'
A string does not have .contents, because it can’t contain anything:
text = title_tag.contents[0]
text.contents
# AttributeError: 'NavigableString' object has no attribute 'contents'
如果一个soup对象里面包含了html 标签,那么string是为None的。不管html tag前面是否有string。
soup=BeautifulSoup("<head><title>The Dormouse's story</title></head>")
head=soup.head
print head.string
输出None说明了这个问题
Instead of getting them as a list, you can iterate over a tag’s children using the .children generator:
for child in title_tag.children:
print(child)
# The Dormouse's story 一个递归获取文本的函数:
def gettextonly(self,soup):
v=soup.string
if v==None:
c=soup.contents
resulttext=''
for t in c:
subtext=self.gettextonly(t)
resulttext+=subtext+'\n'
return resulttext
else:
return v.strip()
一个分割字符串为单词的函数:
def separatewords(self,text):
splitter=re.compile('\\W')
return [s.lower() for s in splitter.split(text) if s!='']
python 模块BeautifulSoup使用的更多相关文章
- 搭建基于python +opencv+Beautifulsoup+Neurolab机器学习平台
搭建基于python +opencv+Beautifulsoup+Neurolab机器学习平台 By 子敬叔叔 最近在学习麦好的<机器学习实践指南案例应用解析第二版>,在安装学习环境的时候 ...
- 50个很棒的Python模块
50个很棒的Python模块 我很喜欢Python,Python具有强大的扩展能力,我列出了50个很棒的Python模块,包含几乎所有的需要:比如Databases,GUIs,Images, Soun ...
- python模块大全
python模块大全2018年01月25日 13:38:55 mcj1314bb 阅读数:3049 pymatgen multidict yarl regex gvar tifffile jupyte ...
- [转]Python 模块收集
Python 模块收集 转自:http://kuanghy.github.io/2017/04/04/python-modules Python | Apr 4, 2017 | python 工具 a ...
- Python 模块 re (Regular Expression)
使用 Python 模块 re 实现解析小工具 概要 在开发过程中发现,Python 模块 re(Regular Expression)是一个很有价值并且非常强大的文本解析工具,因而想要分享一下此 ...
- python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheetahcherrypy:一个WEB frameworkctype ...
- [转]50个很棒的Python模块
转自:http://www.cnblogs.com/foxhengxing/archive/2011/07/29/2120897.html Python具有强大的扩展能力,以下列出了50个很棒的Pyt ...
- 常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件 bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheetahcherrypy:一个WEB frameworkctyp ...
- 使用C/C++写Python模块
最近看开源项目时学习了一下用C/C++写python模块,顺便把学习进行一下总结,废话少说直接开始: 环境:windows.python2.78.VS2010或MingW 1 创建VC工程 (1) 打 ...
随机推荐
- Ubuntu 用 pptp 建立 vpn 服务
1.下载pptp sudo apt-get install pptpd 2.配置pptp 须要改动配置下面的文件: pptpd.conf文件:配置链接后的主机ip和能够分配的内存范围 vi /etc/ ...
- VIM在文件夹中查找
在vim中提供2中方法来在其他文件或者文件夹中搜索字符串,第一种是vimgrep还有一种是grep. 如果只是在当前打开的文件中查找字符串的,使用 :? 后面加上想要搜索的字符串就可以. 这里要解决的 ...
- js页面loading加载
<html> <head> <title>页面正在载入</title> <script language=" ...
- hibernate通过配置文件生成数据库信息
hibernate可以通过配置文件在数据库生成相应的数据库信息.也可以把数据库的信息生成相应的代码(实体类操作类和映射文件) 下面是通过代码默认对hibernate.cfg.xml信息在数据库生成信息 ...
- Cocos2d-x基础篇C++
1.C++类和对象 类的公有成员可以使用成员访问运算符(.)访问. (::)是范围解析运算符.调用成员函数是在对象上使用(.)运算符. 2.C++继承(C++中父类称为基类,子类称为派生类) clas ...
- c++ 深浅拷贝
对于普通类型的对象来说,它们之间的复制是很简单的,例如:int a=88;int b=a;而类对象与普通对象不同,类对象内部结构一般较为复杂,存在各种成员变量.下面看一个类对象拷贝的简单例子. #in ...
- SignaLR通信技术
新建MVC项目 如果没有Signale需要使用NuGet安装Signalr namespace SignaLrDemo { public class ChatHub : Hub { public vo ...
- 采用translate实现垂直水平居中
今天分享一个利用css3新特性实现垂直水平居中的方法. 通过对元素进行绝对定位再配合transform中的translate实现. 代码如下: html <div id="conten ...
- 微信公众号token验证失败的一些总结
这几天准备弄一个微信公众号,在进行服务器配置的时候出现总是出现token验证失败的报错. 实际上,这个问题很好解决.既然微信平台没有给我们很明确的报错提示,那么我们就可以通过跟踪获取到的请求参数进行分 ...
- 【转】AC算法详解
原文转自:http://blog.csdn.net/joylnwang/article/details/6793192 AC算法是Alfred V.Aho(<编译原理>(龙书)的作者),和 ...