打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
python-31: 极视界大图爬取
#!/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
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Python爬取网易云音乐辑的图片、专辑名和专辑出版时间
使用Python画股票的K线图的方法步骤
Python中re(正则表达式)模块详解
python实现简单爬虫以及正则表达式简述
Python网络爬虫(一)
python爬虫
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服