爬虫

爬取昵图网图片并保存

# -*- coding: utf-8 -*-
import re
import urllib

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

def getImg(html,p):
    reg = r'<img src="(.+\.jpg)"'
    imgre = re.compile(reg)
    imglist = re.findall(imgre,html)
    x = 0;
    for imgurl in imglist:
        y = str(p) + '-'+ str(x);
        urllib.urlretrieve(imgurl,'./images/%s.jpg' % y)
        x+=1 


for i in range(19,60 +1):
    url = "http://www.nipic.com/zhuanti/817811_"+ str(i) +".html"
    print url
    html = getHtml(url)
    print getImg(html,p=i)

 

posted @ 2016-11-03 21:01  brady-wang  阅读(139)  评论(0编辑  收藏  举报