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.

  1. 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>]
  2. 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']
  3. 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提供了findfind_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)的形式,来按标签的属性查找。键-值查找里有比较多的小花招,这里列几条:

    1. class
      class是Python的保留字,不能当变量名用,偏偏在HTML中会有很多 class=XXX 的情况,BeautifulSoup的解决方法是加一下划线,用 class_ 代替,如 tag(class_=XXX)
    2. True
      当值为True时,会匹配所有带这个键的标签,如 tag(href=True)
    3. text
      text做为键时表示查找按标签中的文本查找,如 tag(text=something)
  • 正则表达式: 例如 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使用的更多相关文章

  1. 搭建基于python +opencv+Beautifulsoup+Neurolab机器学习平台

    搭建基于python +opencv+Beautifulsoup+Neurolab机器学习平台 By 子敬叔叔 最近在学习麦好的<机器学习实践指南案例应用解析第二版>,在安装学习环境的时候 ...

  2. 50个很棒的Python模块

    50个很棒的Python模块 我很喜欢Python,Python具有强大的扩展能力,我列出了50个很棒的Python模块,包含几乎所有的需要:比如Databases,GUIs,Images, Soun ...

  3. python模块大全

    python模块大全2018年01月25日 13:38:55 mcj1314bb 阅读数:3049 pymatgen multidict yarl regex gvar tifffile jupyte ...

  4. [转]Python 模块收集

    Python 模块收集 转自:http://kuanghy.github.io/2017/04/04/python-modules Python | Apr 4, 2017 | python 工具 a ...

  5. Python 模块 re (Regular Expression)

    使用 Python 模块 re 实现解析小工具   概要 在开发过程中发现,Python 模块 re(Regular Expression)是一个很有价值并且非常强大的文本解析工具,因而想要分享一下此 ...

  6. python开发_常用的python模块及安装方法

    adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheetahcherrypy:一个WEB frameworkctype ...

  7. [转]50个很棒的Python模块

    转自:http://www.cnblogs.com/foxhengxing/archive/2011/07/29/2120897.html Python具有强大的扩展能力,以下列出了50个很棒的Pyt ...

  8. 常用的python模块及安装方法

    adodb:我们领导推荐的数据库连接组件 bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheetahcherrypy:一个WEB frameworkctyp ...

  9. 使用C/C++写Python模块

    最近看开源项目时学习了一下用C/C++写python模块,顺便把学习进行一下总结,废话少说直接开始: 环境:windows.python2.78.VS2010或MingW 1 创建VC工程 (1) 打 ...

随机推荐

  1. BC第二场

    GT and sequence  Accepts: 385  Submissions: 1467  Time Limit: 2000/1000 MS (Java/Others)  Memory Lim ...

  2. 极客Web前端开发资源大荟萃

    前端开发已经成为当前炙手可热的技术之中的一个.本周我们除了给大家带技术相关资讯,另一些技术人员经常使用的站点.希望大家不要错过我们本周的内容.原文来自:极客标签 为神马说敲代码是非常艰难的 程序猿 做 ...

  3. Easyui登陆页面制作

    一.登陆页面HTML <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Web ...

  4. flexbox 伸缩布局盒

    Flexbox(伸缩布局盒) 是 CSS3 中一个新的布局模式,为了现代网络中更为复杂的网页需求而设计. Flexbox 由 伸缩容器 和 伸缩项目 组成.通过设置元素的 display 属性为   ...

  5. 用jersey + spring 实现rest服务及单元测试

    jersey提供了强大的rest功能,可以通过简洁的标注和编码实现业务的需求,架构会透明的把你的pojo对象转化为客户端可以接受的json/xml文件模式,当然也可以用它做一些基于ajax的表单提交和 ...

  6. CSS3滤镜

    今天在办公室亲眼目睹了同事使用CSS3滤镜为一张漂亮的照片轮廓加上了阴影,瞬间亮瞎了我的的双眼,见笑了. 所以也迅速尝试使用CSS3滤镜让最新出炉的MUI LOGO也性感一把,试图来愉悦一下大家的双眼 ...

  7. java思考题

    1.仔细阅读示例: EnumTest.java,运行它,分析运行结果? 你能得到什么结论?你掌握了枚举类型的基本用法了吗? public class EnumTest { public static ...

  8. PHP冒泡排序法

    冒泡排序(Bubble Sort),是一种计算机科学领域的较简单的排序算法.法如其名,就是像冒泡一样,每次从数组当中 冒一个最大的数出来. 冒泡排序它重复地走访过要排序的数列,一次比较两个元素,如果他 ...

  9. IntelliJ IDEA中创建并运行scala应用程序

    1.安装scala插件 2.创建scala项目 下载scala SDK,如果你已经下载了,选择你所下载的版本,点击OK

  10. pyhton10min系列之程序员的浪漫-足迹生成器,有视频教程

    记录去过的足迹 本文为原创文章 项目主页 跪求star 程序员的浪漫,我女朋友蘑菇喜欢旅游,于是我做了这个,记录2015一起去过的地方,祝她圣诞快乐 如果觉得对你有帮助,github求个star 视频 ...