1 #!/usr/bin/env python
2 #-*- coding: utf-8 -*-
3
4 import json
5 import urllib
6 import requests
7
8
9 # 指定url路径,经过查看url获取的信息:0/0是全部的json信息
10 pre_url = "http://gank.io/api/data/福利/0/0"
11
12 # http://gank.io/
13 # 分类数据: http://gank.io/api/data/数据类型/请求个数/第几页
14 # 数据类型: 福利 | Android | iOS | 休息视频 | 拓展资源 | 前端 | all
15 # http://gank.io/api/data/福利/10/1
16 # 请求个数: 数字,大于0
17 # 第几页:数字,大于0
18
19
20
21 # 图片路径列表,有些图片地址是https的,爬取的话需要验证证书,不爬取https开头的图片
22 http_image_list = []
23 #https_image_list = []
24
25 # 1.获取路径的json数据
26 response = requests.get(pre_url).text
27 # 2.jsong格式转换成字符串
28 data_dict = json.loads(response)
29 # 3.获取图片地址数量
30 num = len(data_dict['results'])
31 for i in range(0,num):
32 url_web = data_dict['results'][i]['url']
33 # http开头的图片路径
34 if url_web.split(':')[0] == 'http':
35 http_image_list.append(url_web)
36 # https开头的图片路径
37 # if url_web.split(':')[0] == 'https':
38 # https_image_list.append(url_web)
39
40 # 保存图片地址为文件
41 with open('http_image.txt','w') as obj:
42 data = json.dumps(http_image_list)
43 obj.write(data)
44
45 # with open('https_image.txt','w') as obj:
46 # data = json.dumps(https_image_list)
47 # obj.write(data)
48
49 # 图片保存路径
50 http_save_path = "D:\zipai\"
51
52 # 具体格式可以调整
53 for x in range(0, 304):
54 urllib.request.urlretrieve(http_image_list[x], http_save_path + str(x) + '.jpg')
55 print('保存http第%s张图片' % x)
56
57 # 网站有反爬机制,爬取到303个时被拦截报错,此时,翻转过来,倒着爬取
58 rev = http_image_list[::-1]
59
60 for x in range(0, 267):
61 urllib.request.urlretrieve(rev[x], http_save_path + str(x+304) + '.jpg')
62 print('保存http第%s张图片' % x)