python 爬蟲入門1 網頁圖片保存

coding=utf-8#coding=utf-8

import urllib
import re

def getHtml(url):
page = urllib.urlopen(url)
html = page.read()
return html

def getImg(html):
reg = r’src=”(.+?.jpg)”’
imgre = re.compile(reg)
imglist = re.findall(imgre,html)
x = 0
for imgurl in imglist:
urllib.urlretrieve(imgurl,’%s.jpg’ % x)
x+=1
return imglist

html = getHtml(“http://www.cocoachina.com/bbs/read.php?tid=182334&page=1“)

print getImg(html)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章