pic_scrapy_python
# _*_ 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的更多相关文章
随机推荐
- xshell帮助
查看内置命令 $help Internal Commands:new: Creates a new session.open: Opens a session or the session dialo ...
- centos7下git服务器端搭建
git的安装: yum 源仓库里的 Git 版本更新不及时,最新版本的 Git 是 1.8.3.1,但是官方最新版本已经到了 2.9.2.想要安装最新版本的的 Git,只能下载源码进行安装. 1. 查 ...
- Pandas 常见的基本方法
说明:文章所有内容均截选自实验楼教程[Pandas 使用教程],想要查看教程完整内容,点击教程即可~ 前言: Pandas 是非常著名的开源数据处理工具,我们可以通过它对数据集进行快速读取.转换.过滤 ...
- 不同平台windows、linux、mac 上换行符的问题
http://blog.chinaunix.net/uid-26748613-id-3179595.html?page=2 https://blog.csdn.net/changruihe/artic ...
- 如何更新clob类型的内容
一.手工测试流程: 1.测试需求(待执行用例) 2.标准化测试流程 1/5 --修改 result_info --用户风控信息表 select sysdate,t.*,t.rowid from fin ...
- Redis:C#使用Redis(1)
一.安装 1.下载安装包: 官方网站:redis.io 官方推荐windows版本:https://github.com/MSOpenTech/redis 2:下载压缩包,解压后如下 redis-se ...
- jmeter压测之 监控--nmon
压测方法整理: 1. 写jmx脚本,整理csv数据文件: 2. 部署测试环境,把jmx和csv放在压测机,把监控脚本nmon放被压测机: 3. 安装nmon: a. w ...
- shell编程:for循环
有几个参数执行几次 do done取代了{} 这种用于 文件的个数,用户的个数等. (())里才可以进行加减乘除.
- jQuery-动画点击淡化消失
<!DOCTYPE html> <html> <head> <title> new document </title> <meta c ...
- Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
严重: Exception sending context initialized event to listener instance of class org.springframework.we ...