-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdownFile.py
36 lines (30 loc) · 1.13 KB
/
downFile.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
__author__ = 'zhangxa'
from curl import Curl
import pycurl
from log4Spider.log4s import Log4Spider
"""
A class used to down an pictures which contains 'jpg','png','gif'
"""
class DownFile:
def __init__(self):
self.filename = ""
def getFileNameByUrl(self,url):
splits = url.split('/')
for step in splits:
for suffix in ('jpg','png','gif'):
if suffix in step:
self.filename = step[:step.rfind('.%s'%suffix)]+".%s"%suffix
break
def saveFile2Local(self,url):
self.getFileNameByUrl(url)
if self.filename:
with open(self.filename,"wb") as output:
curl = Curl()
curl.set_url(url)
curl.set_option(pycurl.WRITEFUNCTION,output.write)
curl.get()
curl.close()
Log4Spider.downLog(self,"downloaded a file:[[[",self.filename,"]]]")
if __name__ == "__main__":
downf = DownFile()
downf.saveFile2Local("http://upload-images.jianshu.io/upload_images/1679702-7e810a34f3ef8d18.jpg?imageMogr2/auto-orient/strip%7CimageView2/1/w/300/h/300")