# _*_ coding:UTF-8 _*_
import requests,json,time,sys
from contextlib import closing
class get_photos(object):
def __init__(self):
self.photos_id = []
self.download_server = 'https://unsplash.com/photos/xxx/download?force=true'
self.target = 'https://unsplash.com/napi/feeds/home'
self.headers = {'authorization': 'Client-ID c94869b36aa272dd62dfaeefed769d4115fb3189a9d1ec88ed457207747be626'} #获取图片ID
def get_ids(self):
req = requests.get(url = self.target,headers = self.headers,verify = False)
html = json.loads(req.text)
next_page = html['next_page']
print(next_page)
for each in html['photos']:
self.photos_id.append(each['id'])
time.sleep(1)
for i in range(4):
req = requests.get(url = next_page,headers = self.headers,verify = False)
html = json.loads(req.text)
next_page = html['next_page']
for each in html['photos']:
self.photos_id.append(each['id'])
time.sleep(1)
#图片下载
def download(self,photo_id,filename):
headers = {'User-Agent':' Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'}
target = self.download_server.replace('xxx',photo_id)
with closing(requests.get(url = target,stream = True,verify = False,headers = self.headers)) as r:
with open('%d.jpg'%filename,'ab+') as f:
for chunk in r.iter_content(chunk_size = 1024):
if chunk:
f.write(chunk)
f.flush()
if __name__ == '__main__':
gp = get_photos()
print('获取图片链接中:')
gp.get_ids()
print('图片下载中:')
for i in range(len(gp.photos_id)):
print('正在下载第%d张图片'%(i+1))
gp.download(gp.photos_id[i],(i+1))

pic_scrapy_python的更多相关文章

随机推荐

  1. Raid5之后安装系统,挂载磁盘

    配置RAID5 略 2.U盘安装centos系统(我的版本是centos7.0) 这一步骤网上很多,可以参考这篇:http://jingyan.baidu.com/article/359911f571 ...

  2. 通过wui登陆 sap 页面对数据进行高级 搜索

    1: 登陆QGL系统. 在 T-CODE搜索框输入wui 会跳到搜索的web页面,进行搜索. 或者浏览器输入: https://ldciqgl.wdf.sap.corp:44300/sap(bD1lb ...

  3. Spark log4j日志配置详解(转载)

    一.spark job日志介绍    spark中提供了log4j的方式记录日志.可以在$SPARK_HOME/conf/下,将 log4j.properties.template 文件copy为 l ...

  4. nodejs+mysql入门实例(表的查询)

    //连接数据库 var mysql = require('mysql'); var connection = mysql.createConnection({ host: '******', //数据 ...

  5. keras 分类回归 损失函数与评价指标

    1.目标函数 (1)mean_squared_error / mse 均方误差,常用的目标函数,公式为((y_pred-y_true)**2).mean()(2)mean_absolute_error ...

  6. php __FILE__ symlink

    定义 __FILE__ 是一个魔法变量(预定义常量),当前运行文件的完整路径(真是文件路径,非软链路径)和文件名.如果用在被包含文件中,则返回被包含的文件名. 官方解释:  __FILE__  文件的 ...

  7. rosetta mpi编译时出现 MPI has not been declared 错误

    安装openmpi2.1.0版本,编译mpi rosetta时出现 MPI has not been declared 错误,经过一系列的摸索发现安装openmpi-1.6.5就可以顺利编译,降低版本 ...

  8. iOS UI基础-9.0 UITableView基础

    在iOS中,要实现表格数据展示,最常用的做法就是使用UITableView.UITableView继承自UIScrollView,因此支持垂直滚动,而且性能极佳. UITableView有两种样式: ...

  9. AI案列 三条路

    在生成点生成带有颜色的小球,小球走对应颜色的路 先做好三个小球,做成预制体 对应颜色的小球,除了自身颜色,其他颜色不要选 新建脚本: using System.Collections; using S ...

  10. C#7.0新特性(VS2017可用)

    分享一下其实2016年12月就已经公布了的C#7.0的新特性吧,虽然很早就出来了,但咱这IDE不支持啊.. 不过在昨天的VS2017中已经完美可以支持使用了. E文好的,移步官方介绍地址:https: ...