#!/usr/bin/env Python
#coding=utf-8
import time
import datetime
import sys
import random
import math
import uuid
import cookielib
import urllib2
import os
class GetImage():
reload(sys)
sys.setdefaultencoding('utf8')
'''
抓取网页文件内容,保存到内存
@url 欲抓取文件 ,path+filename
'''
def get_file(self,url):
try:
cj=cookielib.LWPCookieJar()
opener=urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
urllib2.install_opener(opener)
req=urllib2.Request(url)
operate=opener.open(req)
data=operate.read()
return data
except BaseException, e:
print e
return None
'''
保存文件到本地
@path 本地路径
@file_name 文件名
@data 文件内容
'''
def save_file(self,file_name, data):
if data == None:
return
file=open(file_name, "wb")
file.write(data)
file.flush()
file.close()
def save_png_file(self,filename,url):
self.save_file(filename,self.get_file(url))
if __name__=="__main__":
h1 = GetImage()
#h1.save_file('c:/log/124.png',h1.get_file('https://www.easck.com/d/file/200619/202006190639462501.jpg'))
#url = 'https://www.easck.com/d/file/200619/202006190639462501.jpg'
#file_path ='c:/log/125.png'
#h1.save_png_file(file_path,url)