首页 > 代码库 > 图片爬虫

图片爬虫

# -*- coding: utf-8 -*-
import re
import urllib
import os.path

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

def getImg(html,p):
    reg = r<img src="http://www.mamicode.com/(http://img/.7160/.com.+/.jpg)" 
    imgre = re.compile(reg)
    imglist = re.findall(imgre,html)
    x = 0;
    for imgurl in imglist:
        print os.path.basename(imgurl)
        y = str(p) + -+ str(x);        
        urllib.urlretrieve(imgurl,./images/%s.jpg % y)
        x+=1
        print y + ".jpg" 
        


for i in range(2,36 +1):
    url = "http://www.7160.com/meinv/38343/index_"+ str(i) +".html"
    print url
    html = getHtml(url)
    print getImg(html,p=i)

 

图片爬虫