if tags==None: final_folder = './Konachan/' else: final_folder = './Konachan/'+tags+'/'
os.makedirs(final_folder, exist_ok=True)
if pages_num<11486: pages_num=pages_num+1 else: pages_num=11487
for n inrange(1,pages_num): url_com='https://konachan.com/post.json?page='+str(n)+'&tags='+tags url_net='https://konachan.com/post.json?page='+str(n)+'&tags='+tags req=requests.get(url_net,headers=headers,proxies=proxies_v2ray).content jsonlist=json.loads(req) iflen(jsonlist)==0: break for i inrange(len(jsonlist)): id_number=jsonlist[i]['id'] file_suffix = os.path.splitext(jsonlist[i]['file_url'])[1] if os.access(final_folder+'%s'%id_number+file_suffix, os.F_OK): print('图片{}存在,跳过'.format(id_number)) else: r=requests.get(jsonlist[i]['file_url'],headers=headers,proxies=proxies_v2ray) print('下载{}图片'.format(id_number)) withopen(final_folder+'%s'%id_number+file_suffix, 'wb') as f: f.write(r.content) print('进入休眠3s') time.sleep(3)
pages=input('Input:请输入您爬取的页数,默认为所有:') if pages == '': pages_num=pages_num+1 else: pages_num_input=int(pages) if pages_num_input<pages_num: pages_num=pages_num_input+1 else: pages_num=pages_num+1 print('共爬取{}页'.format(pages_num))
for n inrange(1,pages_num): url_com='https://wallhaven.cc/api/v1/search?sorting=toplist&page='+str(n)+'&apikey='+api_key req=requests.get(url_com,headers=headers,proxies=proxies_v2ray).content jsonlist=json.loads(req) jsonlist_data=jsonlist['data'] for i inrange(len(jsonlist_data)): id_number=jsonlist_data[i]['id'] file_suffix = os.path.splitext(jsonlist_data[i]['path'])[1] if os.access(final_folder+'%s'%id_number+file_suffix, os.F_OK): print('图片{}存在,跳过'.format(id_number)) else: r=requests.get(jsonlist_data[i]['path'],headers=headers,proxies=proxies_v2ray) print('下载{}图片'.format(id_number)) withopen(final_folder+'%s'%id_number+file_suffix, 'wb') as f: f.write(r.content) print('进入休眠3s') time.sleep(3)