Python 插件杂谈 (4) ---- BeautifulSoup , Python中的网页分析工具
2010-08-15 08:57
525 查看
嗯哼,Meego中文核心站
-- 米趣网
又发新博文啦。
前面向大家介绍了 PyQuery
,下面转而介绍一下 BeautifulSoup
, Beautiful Soup 是 Python 内置的网页分析工具,名字叫美丽的蝴蝶。呵呵,某些时候确如美丽蝴蝶一样。
先来段介绍:
Beautiful Soup
是一个 Python HTML/XML 处理器,设计用来快速地转换网页抓取。以下的特性支撑着 Beautiful Soup
:
Beautiful Soup
不会选择 即使你给他一个损坏的标签。 他产生一个转换DOM树,尽可能和你原文档内容含义一致 。这种措施通常能够你搜集数据的需求。
Beautiful Soup
提供一些简单的方法以及类Python语法 来查找、查找、修改一颗转换树:一个工具集帮助你解析一棵树并释出你需要的内容。你不需要为每一个应用创建自己的解析工具。
Beautiful Soup
自动
将送进来的文档转换为 Unicode 编码
而且在输出的时候转换为 UTF-8,。 除非这个文档没有指定编码方式或者Beautiful Soup 没能自动检测编码,你需要手动指定编码方式,否则你不需要考虑编码的问题。
Beautiful Soup
转换任何你给他的内容,然后为你做那些转换的事情。你可以命令他 “找出所有的链接", 或者 "找出所有 class 是 externalLink
的链接" , 再或者是 "找出所有的链接 url 匹配 ”foo.com", 甚至是 "找出那些表头是粗体文字,然后返回给我文字“.
那些设计不好的网站中的有价值的数据可以被你一次锁定,原本要花数个小时候的工作,通过使用 Beautiful Soup 可以在几分钟内搞定。
下面让我们快速开始:
首先引用包:
from BeautifulSoup import BeautifulSoup # For processing HTML
from BeautifulSoup import BeautifulStoneSoup # For processing XML
import BeautifulSoup # To get everything[/font][/color]
复制代码
下面使用一段代码演示Beautiful Soup的基本使用方式。你可以拷贝与粘贴这段代码自己运行。
from BeautifulSoup import BeautifulSoup
import re
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>']
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>
复制代码
下面是一个解析文档的方法:
soup.contents[0].name
# u'html'
soup.contents[0].contents[0].name
# u'head'
head = soup.contents[0].contents[0]
head.parent.name
# u'html'
head.next
# <title>Page title</title>
head.nextSibling.name
# u'body'
head.nextSibling.contents[0]
# <p id="firstpara" align="center">This is paragraph <b>one</b>.</p>
head.nextSibling.contents[0].nextSibling
# <p id="secondpara" align="blah">This is paragraph <b>two</b>.</p>
复制代码
接着是一打方法查找一文档中包含的标签,或者含有指定属性的标签
titleTag = soup.html.head.title
titleTag
# <title>Page title</title>
titleTag.string
# u'Page title'
len(soup('p'))
# 2
soup.findAll('p', align="center")
# [<p id="firstpara" align="center">This is paragraph <b>one</b>. </p>]
soup.find('p', align="center")
# <p id="firstpara" align="center">This is paragraph <b>one</b>. </p>
soup('p', align="center")[0]['id']
# u'firstpara'
soup.find('p', align=re.compile('^b.*'))['id']
# u'secondpara'
soup.find('p').b.string
# u'one'
soup('p')[1].b.string
# u'two'
复制代码
当然也可以简单地修改文档
titleTag['id'] = 'theTitle'
titleTag.contents[0].replaceWith("New title")
soup.html.head
# <head><title id="theTitle">New title</title></head>
soup.p.extract()
soup.prettify()
# <html>
# <head>
# <title id="theTitle">
# New title
# </title>
# </head>
# <body>
# <p id="secondpara" align="blah">
# This is paragraph
# <b>
# two
# </b>
# .
# </p>
# </body>
# </html>
soup.p.replaceWith(soup.b)
# <html>
# <head>
# <title id="theTitle">
# New title
# </title>
# </head>
# <body>
# <b>
# two
# </b>
# </body>
# </html>
soup.body.insert(0, "This page used to have ")
soup.body.insert(2, " <p> tags!")
soup.body
# <body>This page used to have <b>two</b> <p> tags!</body>
复制代码
最后,为大家提供 Beautiful Soup
的文档。希望能对您有帮助。
转载文章 ,请注明来自 米趣网
-- 米趣网
又发新博文啦。
前面向大家介绍了 PyQuery
,下面转而介绍一下 BeautifulSoup
, Beautiful Soup 是 Python 内置的网页分析工具,名字叫美丽的蝴蝶。呵呵,某些时候确如美丽蝴蝶一样。
先来段介绍:
Beautiful Soup
是一个 Python HTML/XML 处理器,设计用来快速地转换网页抓取。以下的特性支撑着 Beautiful Soup
:
Beautiful Soup
不会选择 即使你给他一个损坏的标签。 他产生一个转换DOM树,尽可能和你原文档内容含义一致 。这种措施通常能够你搜集数据的需求。
Beautiful Soup
提供一些简单的方法以及类Python语法 来查找、查找、修改一颗转换树:一个工具集帮助你解析一棵树并释出你需要的内容。你不需要为每一个应用创建自己的解析工具。
Beautiful Soup
自动
将送进来的文档转换为 Unicode 编码
而且在输出的时候转换为 UTF-8,。 除非这个文档没有指定编码方式或者Beautiful Soup 没能自动检测编码,你需要手动指定编码方式,否则你不需要考虑编码的问题。
Beautiful Soup
转换任何你给他的内容,然后为你做那些转换的事情。你可以命令他 “找出所有的链接", 或者 "找出所有 class 是 externalLink
的链接" , 再或者是 "找出所有的链接 url 匹配 ”foo.com", 甚至是 "找出那些表头是粗体文字,然后返回给我文字“.
那些设计不好的网站中的有价值的数据可以被你一次锁定,原本要花数个小时候的工作,通过使用 Beautiful Soup 可以在几分钟内搞定。
下面让我们快速开始:
首先引用包:
from BeautifulSoup import BeautifulSoup # For processing HTML
from BeautifulSoup import BeautifulStoneSoup # For processing XML
import BeautifulSoup # To get everything[/font][/color]
复制代码
下面使用一段代码演示Beautiful Soup的基本使用方式。你可以拷贝与粘贴这段代码自己运行。
from BeautifulSoup import BeautifulSoup
import re
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>']
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>
复制代码
下面是一个解析文档的方法:
soup.contents[0].name
# u'html'
soup.contents[0].contents[0].name
# u'head'
head = soup.contents[0].contents[0]
head.parent.name
# u'html'
head.next
# <title>Page title</title>
head.nextSibling.name
# u'body'
head.nextSibling.contents[0]
# <p id="firstpara" align="center">This is paragraph <b>one</b>.</p>
head.nextSibling.contents[0].nextSibling
# <p id="secondpara" align="blah">This is paragraph <b>two</b>.</p>
复制代码
接着是一打方法查找一文档中包含的标签,或者含有指定属性的标签
titleTag = soup.html.head.title
titleTag
# <title>Page title</title>
titleTag.string
# u'Page title'
len(soup('p'))
# 2
soup.findAll('p', align="center")
# [<p id="firstpara" align="center">This is paragraph <b>one</b>. </p>]
soup.find('p', align="center")
# <p id="firstpara" align="center">This is paragraph <b>one</b>. </p>
soup('p', align="center")[0]['id']
# u'firstpara'
soup.find('p', align=re.compile('^b.*'))['id']
# u'secondpara'
soup.find('p').b.string
# u'one'
soup('p')[1].b.string
# u'two'
复制代码
当然也可以简单地修改文档
titleTag['id'] = 'theTitle'
titleTag.contents[0].replaceWith("New title")
soup.html.head
# <head><title id="theTitle">New title</title></head>
soup.p.extract()
soup.prettify()
# <html>
# <head>
# <title id="theTitle">
# New title
# </title>
# </head>
# <body>
# <p id="secondpara" align="blah">
# This is paragraph
# <b>
# two
# </b>
# .
# </p>
# </body>
# </html>
soup.p.replaceWith(soup.b)
# <html>
# <head>
# <title id="theTitle">
# New title
# </title>
# </head>
# <body>
# <b>
# two
# </b>
# </body>
# </html>
soup.body.insert(0, "This page used to have ")
soup.body.insert(2, " <p> tags!")
soup.body
# <body>This page used to have <b>two</b> <p> tags!</body>
复制代码
最后,为大家提供 Beautiful Soup
的文档。希望能对您有帮助。
转载文章 ,请注明来自 米趣网
相关文章推荐
- Python 插件杂谈 (3) ---- PyQuery , Python中的网页分析利器
- python下分析网页的好工具:BeautifulSoup
- Python获取网页指定内容(BeautifulSoup工具的使用方法)
- python使用BeautifulSoup分析网页信息的方法
- python beautifulsoup多线程分析抓取网页
- 【工具】网页性能分析工具-火狐浏览器YSlow插件的使用
- xpath helper插件:网页爬虫分析工具
- 2.2 Xpath-helper (chrome插件) 爬虫、网页分析解析辅助工具
- python beautifulsoup多线程分析抓取网页
- python beautifulsoup多线程分析抓取网页
- python beautifulsoup多线程分析抓取网页
- Python获取网页指定内容(BeautifulSoup工具的使用方法)
- Python 插件杂谈 (1) ---- chardet
- python使用HTMLParser和BeautifulSoup解析网页
- Python学习 - 使用BeautifulSoup来解析网页一:基础入门
- python-16:分析网页源代码
- Python3爬虫之六网页的初步分析【抓取豆瓣最新电影影评并用词云显示】
- Python构建网页爬虫原理分析
- 【脚本语言系列】关于Python代码分析工具line_profiler,你需要知道的事
- python 抓取网页--用BeautifulSoup