Parse The Web Using Python+Beautiful Soup
-
Upload
jim-chang -
Category
Technology
-
view
14.024 -
download
0
description
Transcript of Parse The Web Using Python+Beautiful Soup
Parse the webusing Python + Beautiful Soup
張竟 at ncucccwebb(dot)tw(at)gmail(dot)com
2009年5月26日星期二
Agenda
• 工具決定• Python簡介
• Beautiful Soup簡介
2009年5月26日星期二
Parse the web?but how?
2009年5月26日星期二
Solutions
• C++
• Java
• Perl
• Python
• Others?
2009年5月26日星期二
Solutions (Cont.)
• 直接字串處理• Regular expression
• 現有的Parser
2009年5月26日星期二
So I decide...
2009年5月26日星期二
Python + Beautiful Soup
2009年5月26日星期二
Python + Beautiful Soup 看第一頁就知道了
2009年5月26日星期二
Python 簡介
• high-level programming language
• scripting language
• 傳說中Google最愛用的語言
2009年5月26日星期二
特色
• 變數不用宣告• 用縮排取代{}
• list tuple dictionary
2009年5月26日星期二
list
• a=[‘asdf’,123,12.01,‘abcd’]
• a[3] (a[-1])
• 12.01
• a[0:2] (a[:2])
• [‘asdf’,123,12.01]
• b=[‘asdf’,123,[‘qwer’,12.34]]
2009年5月26日星期二
list (Cont.)
• a=[‘abc’,12]
• len(a)
• #2
• a.append(1)
• #[‘abc’,12,1]
• a.insert(1,‘def’)
• #[‘abc’,‘def’,12,1]
2009年5月26日星期二
list (Cont.)
• a= [321,456,12,1]
• a.pop()
• #[321,456,12]
• a.index(12)
• #2
• a.sort()
• #1,12,321,456]
2009年5月26日星期二
tuple
• a=(‘asdf’,123,12.01) or a= ‘asdf’,123,12.01
• a=((‘abc’,1),123.1)
• a,b=1,2
2009年5月26日星期二
Dictionary
• a={123:‘abc’,‘cde’:456}
• a[123]
• #abc’
• a[‘cde’]
• #456
2009年5月26日星期二
if else
if a>10:print ‘a>10’
elif a<5:print ‘a<5’
else:print ‘5<a<10
2009年5月26日星期二
while loop
while a>2 or a<3:pass
2009年5月26日星期二
for loopa=[‘abc’,123,‘def’]for x in a:
print x
for x in range(3):print x
for x in range(4,34,10):print x
abc123def
012
41424
2009年5月26日星期二
function
def fib(n):if n==0 or n==1:
return nelse:
return fib(n-1),fib(n-2)
2009年5月26日星期二
終於可以進入正題了....
2009年5月26日星期二
What is Beautiful Soup
• 一個python module
• html/xml parser
• 會把html/xml解析成樹狀結構
• 可以對他做搜尋、修改
not Beautiful Soap
2009年5月26日星期二
Beautiful Soup<html> <head> <title> page title </title> </head> <body> <p id="firstpara" align="center"> first paragraph <b> one </b> </p> <p id="secondpara" align="blah"> second paragraph <b> two </b> </p> </body></html>
2009年5月26日星期二
基本操作
from BeautifulSoup import BeautifulSoupsoup=BeautifulSoup(page)
soup.html.head#<head><title>page title</title></head>
soup.head#<head><title>page title</title></head>
soup.body.p#<p id="firstpara" align="center">This is paragraph<b>one</b></p>
check urllib/urllib2 to see how to open a url in python
2009年5月26日星期二
基本操作(Cont.)
• parent (go to parent node)
soup.title.parent == soup.head
• next (go to next node)
soup.title.next == ‘page title’soup.title.next.next == soup.body
• previous (go to previous node)
soup.title.previous == soup.headsopu.body.p.previous == ‘first paragraph’
2009年5月26日星期二
基本操作(Cont.)• contents (all content nodes)
soup.html.contents ==[soup.html.head , soup.html.body]
• nextSibling (go to next sibling)
soup.html.body.p.nextSibling== soup.html.body.contents[1]
• previousSibling (previous sibling)
soup.html.body.previousSibling== soup.html.head
2009年5月26日星期二
基本操作(Cont.)• tag名稱
soup.html.body.name == ‘body’
• 輸出字串soup.html.head.title.string== str(soup.html.head.title)== soup.html.title.head.contents[0]== ‘page title’
• Tag屬性
soup.html.body.p.attrMap== {'align' : 'center', 'id' : 'firstpara'}
soup.html.body.p[‘id’] == 'firstpara'
2009年5月26日星期二
搜尋
• find(name, attrs, recursive, text)
2009年5月26日星期二
搜尋
• find(name, attrs, recursive, text)
tag名稱
2009年5月26日星期二
搜尋
• find(name, attrs, recursive, text)
tag名稱
tag屬性
2009年5月26日星期二
搜尋
• find(name, attrs, recursive, text)
tag名稱
tag屬性
遞迴搜尋
2009年5月26日星期二
搜尋
• find(name, attrs, recursive, text)
tag名稱
tag屬性
遞迴搜尋
tag內容
2009年5月26日星期二
find(name, attrs, recursive, text)
• soup.find(‘p’)
#<p id="firstpara" align="center">This is paragraph<b>one</b></p>
2009年5月26日星期二
find(name, attrs, recursive, text)
soup.find(‘p’) == soup.html.body.p
soup.find(‘p’,id=‘secondpara’)#<p id="secondpara" align="blah">This is paragraph<b>two</b></p>
soup.find(‘p’,recuresive=False)==None
soup.find(text=‘one’)==soup.b.next
2009年5月26日星期二
findAll(name, attrs, recursive, text,limit)
soup.findAll(‘p’) == [soup.html.body.p ,soup.p.nextSibling
soup.findAll(‘p’,id=‘secondpara’)#[<p id="secondpara" align="blah">This is paragraph<b>two</b></p>]
soup.findAll(‘p’,recuresive=False)==[]
soup.findAll(text=‘one’)==soup.b.next
soup.findAll(limit=4)==[soup.html , soup.html.body ,soup.html.body.title , soup.html.body]
2009年5月26日星期二
Other solutions
• lxml
• html5lib
• HTMLParser
• htmlfill
• Genshi
http://blog.ianbicking.org/2008/03/30/python-html-parser-performance/
2009年5月26日星期二
Reference
• Python Official Websitehttp://www.python.com/ (>///< 兒童不宜)
http://www.python.org/
• Beautiful Soup documentationhttp://www.crummy.com/software/BeautifulSoup/
• personal bloghttp://blog.ez2learn.com/2008/10/05/python-is-the-best-choice-to-grab-web/
• Python html parser performancehttp://blog.ianbicking.org/2008/03/30/python-html-parser-performance/
2009年5月26日星期二