Python学习 之 爬虫

目标:下载贴吧或空间中所有图片

步骤:(1)获取页面代码 (2)获取图片URL,下载图片

代码如下:

#!/usr/bin/python
import re
import urllib

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

def getImg(html):
    reg=r'src="(.*?\.jpg)" width'
    imgre=re.compile(reg)
    imglist=re.findall(imgre,html)
    x=0
    for imgurl in imglist:
        urllib.urlretrieve(imgurl,'%s.jpg' % x)
        x+=1

html=getHtml("http://tieba.baidu.com/p/749901827?fr=good")
getImg(html)

 

posted @ 2015-06-22 21:18  sunflower627  阅读(203)  评论(0编辑  收藏  举报