一個從百度貼吧,爬取圖片的例子。
### 包
urllib,urllib2,re
### 函數
page=urllib.urlopen('http://...') ?返回一個網頁對象
html=page.read() ? 讀取網頁的html源碼
urllib.urlretrieve() ?下載資源到本地
### 代碼
~~~
#coding:utf8
import re
import urllib
def getHtml(url):
page=urllib.urlopen(url)
html=page.read()
return html
def getImgUrl(html):
reg=r'src="(.*?\.jpg)"'#?非貪婪匹配,()分組只返回分組結果,外單引號內雙引號
imgre=re.compile(reg)#編譯正則表達式,加快速度
imglist=re.findall(imgre,html)
return imglist
url="http://tieba.baidu.com/p/3162606526"#貼吧地址
html=getHtml(url)
ImgList=getImgUrl(html)
ImgList= ImgList[1:10]#取前10個下載
print ImgList
x=0
for imgurl in ImgList:
urllib.urlretrieve(imgurl,'%s.jpg' %x)
x+=1
~~~