pic_scrapy_python

Posted Edge of Eternity

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了pic_scrapy_python相关的知识,希望对你有一定的参考价值。

 1 # _*_ coding:UTF-8 _*_
 2 import requests,json,time,sys
 3 from contextlib import closing
 4 class get_photos(object):
 5     def __init__(self):
 6         self.photos_id = []
 7         self.download_server = https://unsplash.com/photos/xxx/download?force=true
 8         self.target = https://unsplash.com/napi/feeds/home
 9         self.headers = {authorization: Client-ID c94869b36aa272dd62dfaeefed769d4115fb3189a9d1ec88ed457207747be626}
10 
11     #获取图片ID
12     def get_ids(self):
13         req = requests.get(url = self.target,headers = self.headers,verify = False)
14         html = json.loads(req.text)
15         next_page = html[next_page]
16         print(next_page)
17         for each in html[photos]:
18             self.photos_id.append(each[id])
19         time.sleep(1)
20         for i in range(4):
21             req = requests.get(url = next_page,headers = self.headers,verify = False)
22             html = json.loads(req.text)
23             next_page = html[next_page]
24             for each in html[photos]:
25                 self.photos_id.append(each[id])
26             time.sleep(1)
27     #图片下载
28     def download(self,photo_id,filename):
29         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}
30         target = self.download_server.replace(xxx,photo_id)
31         with closing(requests.get(url = target,stream = True,verify = False,headers = self.headers)) as r:
32             with open(%d.jpg%filename,ab+) as f:
33                 for chunk in r.iter_content(chunk_size = 1024):
34                     if chunk:
35                         f.write(chunk)
36                         f.flush()
37 if __name__ == __main__:
38     gp = get_photos()
39     print(获取图片链接中:)
40     gp.get_ids()
41     print(图片下载中:)
42     for i in range(len(gp.photos_id)):
43         print(正在下载第%d张图片%(i+1))
44         gp.download(gp.photos_id[i],(i+1))

 

以上是关于pic_scrapy_python的主要内容,如果未能解决你的问题,请参考以下文章

VSCode自定义代码片段——CSS选择器

谷歌浏览器调试jsp 引入代码片段,如何调试代码片段中的js

片段和活动之间的核心区别是啥?哪些代码可以写成片段?

VSCode自定义代码片段——.vue文件的模板

VSCode自定义代码片段6——CSS选择器

VSCode自定义代码片段——声明函数