Python爬虫_简单获取百度贴吧图片

来源:互联网 发布:手机上能开淘宝店吗 编辑:程序博客网 时间:2024/05/24 20:07
#coding=utf-8import reimport urllibdef getHtml(url):    page = urllib.urlopen(url)    html = page.read()    return htmldef callbackfunc(blocknum, blocksize, totalsize):    '''回调函数    @blocknum: 已经下载的数据块    @blocksize: 数据块的大小    @totalsize: 远程文件的大小    '''    percent = 100.0 * blocknum * blocksize / totalsize    if percent > 100:        percent = 100    print "%.2f%%"% percentdef getImg(html):    reg = r'src="(.+?\.jpg)" pic_ext'    imgre = re.compile(reg)    imglist = re.findall(imgre,html)    x = 0    #保存文件到本地,urlretrieve函数的第一个参数是URL,第二个是地址    for imgurl in imglist:        print '正在保存第' + str(x+1) + '图片...'        urllib.urlretrieve(imgurl,'%s.jpg' % x,callbackfunc)        x+=1    return imglist         html = getHtml("http://tieba.baidu.com/p/2460150866")#print getImg(html)getImg(html)

0 0
原创粉丝点击