图片爬虫

# -*- 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://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)

 

posted @ 2016-11-29 23:13  brady-wang  阅读(344)  评论(0编辑  收藏  举报