网络编程
位置:首页>> 网络编程>> Python编程>> python保存网页图片到本地的方法

python保存网页图片到本地的方法

作者:huhuliuxia  发布时间:2021-05-20 16:15:04 

标签:python,保存图片

本文实例为大家分享了python保存网页图片到本地的具体代码,供大家参考,具体内容如下


#!/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('http://1.1.1.1/doc/images/public/ICON/norecord.png'))
#url = 'http://1.1.1.1/doc/images/public/ICON/norecord.png'
#file_path ='c:/log/125.png'
#h1.save_png_file(file_path,url)

来源:https://blog.csdn.net/huhuliuxia/article/details/42099475

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com