python-简单爬虫抓取贴吧图片

一个简单的爬虫练习
import urllib.request
import re

def getHtml(url):
    page = urllib.request.urlopen(url)
    #read()返回的结果是二进制的需要转换成字符串
    html =page.read().decode("utf-8")
    return html

def getImage(page):

    reg=r'src="(.+?\.jpg)" size'
    imgre=re.compile(reg)
    print (imgre)
    imglist=re.findall(imgre,page)
    print (imglist)
    x=0
    for i in imglist:
        urllib.request.urlretrieve(i,'%s.jpg' %x)
        x+=1
    return imglist

html=getHtml("http://tieba.baidu.com/p/4721099001")
print(getImage(html))

 

posted @ 2017-01-17 10:11  eva_Tang  阅读(1499)  评论(0编辑  收藏  举报