Parse The Web Using Python+Beautiful Soup

36
Parse the web using Python + Beautiful Soup 張竟 at ncucc cwebb(dot)tw(at)gmail(dot)com 2009年5月26日星期二

description

 

Transcript of Parse The Web Using Python+Beautiful Soup

Page 1: Parse The Web Using Python+Beautiful Soup

Parse the webusing Python + Beautiful Soup

張竟 at ncucccwebb(dot)tw(at)gmail(dot)com

2009年5月26日星期二

Page 2: Parse The Web Using Python+Beautiful Soup

Agenda

• 工具決定• Python簡介

• Beautiful Soup簡介

2009年5月26日星期二

Page 3: Parse The Web Using Python+Beautiful Soup

Parse the web?but how?

2009年5月26日星期二

Page 4: Parse The Web Using Python+Beautiful Soup

Solutions

• C++

• Java

• Perl

• Python

• Others?

2009年5月26日星期二

Page 5: Parse The Web Using Python+Beautiful Soup

Solutions (Cont.)

• 直接字串處理• Regular expression

• 現有的Parser

2009年5月26日星期二

Page 6: Parse The Web Using Python+Beautiful Soup

So I decide...

2009年5月26日星期二

Page 7: Parse The Web Using Python+Beautiful Soup

Python + Beautiful Soup

2009年5月26日星期二

Page 8: Parse The Web Using Python+Beautiful Soup

Python + Beautiful Soup 看第一頁就知道了

2009年5月26日星期二

Page 9: Parse The Web Using Python+Beautiful Soup

Python 簡介

• high-level programming language

• scripting language

• 傳說中Google最愛用的語言

2009年5月26日星期二

Page 10: Parse The Web Using Python+Beautiful Soup

特色

• 變數不用宣告• 用縮排取代{}

• list tuple dictionary

2009年5月26日星期二

Page 11: Parse The Web Using Python+Beautiful Soup

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日星期二

Page 12: Parse The Web Using Python+Beautiful Soup

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日星期二

Page 13: Parse The Web Using Python+Beautiful Soup

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日星期二

Page 14: Parse The Web Using Python+Beautiful Soup

tuple

• a=(‘asdf’,123,12.01) or a= ‘asdf’,123,12.01

• a=((‘abc’,1),123.1)

• a,b=1,2

2009年5月26日星期二

Page 15: Parse The Web Using Python+Beautiful Soup

Dictionary

• a={123:‘abc’,‘cde’:456}

• a[123]

• #abc’

• a[‘cde’]

• #456

2009年5月26日星期二

Page 16: Parse The Web Using Python+Beautiful Soup

if else

if a>10:print ‘a>10’

elif a<5:print ‘a<5’

else:print ‘5<a<10

2009年5月26日星期二

Page 17: Parse The Web Using Python+Beautiful Soup

while loop

while a>2 or a<3:pass

2009年5月26日星期二

Page 18: Parse The Web Using Python+Beautiful Soup

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日星期二

Page 19: Parse The Web Using Python+Beautiful Soup

function

def fib(n):if n==0 or n==1:

return nelse:

return fib(n-1),fib(n-2)

2009年5月26日星期二

Page 20: Parse The Web Using Python+Beautiful Soup

終於可以進入正題了....

2009年5月26日星期二

Page 21: Parse The Web Using Python+Beautiful Soup

What is Beautiful Soup

• 一個python module

• html/xml parser

• 會把html/xml解析成樹狀結構

• 可以對他做搜尋、修改

not Beautiful Soap

2009年5月26日星期二

Page 22: Parse The Web Using Python+Beautiful Soup

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日星期二

Page 23: Parse The Web Using Python+Beautiful Soup

基本操作

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日星期二

Page 24: Parse The Web Using Python+Beautiful Soup

基本操作(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日星期二

Page 25: Parse The Web Using Python+Beautiful Soup

基本操作(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日星期二

Page 26: Parse The Web Using Python+Beautiful Soup

基本操作(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日星期二

Page 27: Parse The Web Using Python+Beautiful Soup

搜尋

• find(name, attrs, recursive, text)

2009年5月26日星期二

Page 28: Parse The Web Using Python+Beautiful Soup

搜尋

• find(name, attrs, recursive, text)

tag名稱

2009年5月26日星期二

Page 29: Parse The Web Using Python+Beautiful Soup

搜尋

• find(name, attrs, recursive, text)

tag名稱

tag屬性

2009年5月26日星期二

Page 30: Parse The Web Using Python+Beautiful Soup

搜尋

• find(name, attrs, recursive, text)

tag名稱

tag屬性

遞迴搜尋

2009年5月26日星期二

Page 31: Parse The Web Using Python+Beautiful Soup

搜尋

• find(name, attrs, recursive, text)

tag名稱

tag屬性

遞迴搜尋

tag內容

2009年5月26日星期二

Page 32: Parse The Web Using Python+Beautiful Soup

find(name, attrs, recursive, text)

• soup.find(‘p’)

#<p id="firstpara" align="center">This is paragraph<b>one</b></p>

2009年5月26日星期二

Page 33: Parse The Web Using Python+Beautiful Soup

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日星期二

Page 34: Parse The Web Using Python+Beautiful Soup

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日星期二

Page 35: Parse The Web Using Python+Beautiful Soup

Other solutions

• lxml

• html5lib

• HTMLParser

• htmlfill

• Genshi

http://blog.ianbicking.org/2008/03/30/python-html-parser-performance/

2009年5月26日星期二

Page 36: Parse The Web Using Python+Beautiful Soup

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日星期二