#!/usr/bin/env python
# -*- coding: UTF-8 -*-
__author__ = '217小月月坑'
'''
网页跳转,相册图片下载
下载大图片
'''
import urllib
import urllib2
import re
import os
# 极视界主页
home_url = 'http://product.yesky.com/more/506001_31372_photograph_1.shtml'
user_agent = 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:40.0) Gecko/20100101 Firefox/40.0'
headers = {'User-Agent':user_agent}
try:
home_request = urllib2.Request(home_url,headers=headers)
home_response = urllib2.urlopen(home_request)
home_conents = home_response.read().decode("gbk")
# 使用RE在主页源码获取相册的网址和相册名字
home_pattern = re.compile(r'<dt><a href="(.*?)" title="(.*?)"',re.S)
# 返回存有相册的网址的列表
album_urls = re.findall(home_pattern,home_conents)
for album_url in album_urls:
print album_url[0],album_url[1]
# 以相册名字创建文件夹
path = r'/home/ym/test/'+album_url[1]
os.mkdir(path)
# 传入相册网址
album_response = urllib2.urlopen(album_url[0])
album_conents = album_response.read().decode("gbk")
album_pattern = re.compile(r'<li >.*?<a href="(.*?)".*?</li>',re.S)
photo_urls = re.findall(album_pattern,album_conents)
i = 1
print u'正在下载'+album_url[1]+u'相册里的图片'
for photo_url in photo_urls:
photo_response = urllib2.urlopen(photo_url)
photo_conents = photo_response.read().decode("gbk")
photo_pattern = re.compile(r'class="effect".*?<img src="(.*?)"',re.S)
img_urls = re.findall(photo_pattern,photo_conents)
for img_url in img_urls:
img_path = path+'/'+str(i)
i+=1
urllib.urlretrieve(img_url,img_path)
print u'图片'+img_path+u'下载完成'
except urllib2.URLError,e:
if hasattr(e,"code"):
print e.code
if hasattr(e,"reason"):
e.reason
联系客服