码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2017-01-17 10:39:20      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:返回   imp   reg   page   class   jpg   image   size   turn   

一个简单的爬虫练习
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=rsrc="(.+?\.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))

 

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

标签:返回   imp   reg   page   class   jpg   image   size   turn   

原文地址:http://www.cnblogs.com/suye/p/6281615.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!