# _*_ 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. Dell R730服务器 Raid0 Raid5配置

    Dell R730服务器,有7块5t硬盘,默认做的RAID5.我们的目的是取其中6块硬盘做RAID5,留一块硬盘做热备. 一块SSD系统盘. 在这里,我具体解释一下 ①6块硬盘做成RAID5 ②6块硬 ...

  2. 546A

    #include <stdio.h> int main() { int n1,n2,n3; int ans; scanf("%d %d %d", &n1, &a ...

  3. Python 命名笔记

    类名开头大写,驼峰命名 函数名,变量名都小写, 全局常量 如COUNT, 使用全部大写 https://www.cnblogs.com/lytwajue/p/7324724.html 这个是全局变量关 ...

  4. node跨域cors模块,nodejs+express跨域

    使用express写的接口,只能在内部使用,如果想要外部的服务访问,就涉及到了跨域.但是又不想用jsonp,其实有一个node模块,可以轻松实现跨域 npm install cors --save 然 ...

  5. 数据分析与挖掘 - R语言:贝叶斯分类算法(案例二)

    接着案例一,我们再使用另一种方法实例一个案例 直接上代码: #!/usr/bin/Rscript library(plyr) library(reshape2) #1.根据训练集创建朴素贝叶斯分类器 ...

  6. vue路由(一个包含重定向、嵌套路由、懒加载的main.js如下)and 路由跳转传参的query和params的异同

    import Vue from 'vue'import VueRouter from 'vue-router'import App from './App'Vue.use(VueRouter)cons ...

  7. hbase-java-api002(flush)

    package api; import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apa ...

  8. Hotfix

    http://group.jobbole.com/6311/ http://www.jianshu.com/p/6f0ae1e364d9 http://www.mamicode.com/info-de ...

  9. 爬取笔下wenxue小说

    import urllib.request from bs4 import BeautifulSoup import re def gethtml(url): page=urllib.request. ...

  10. 获取数据库连接对象Connection

    2018-11-04  19:50:52 开始写 public Connection getConn() {//返回类型为Connection try { Class.forName("co ...