python保存網(wǎng)頁圖片到本地的方法
更新時間:2018年07月24日 14:48:13 作者:huhuliuxia
這篇文章主要為大家詳細介紹了python保存網(wǎng)頁圖片到本地的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了python保存網(wǎng)頁圖片到本地的具體代碼,供大家參考,具體內(nèi)容如下
#!/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')
'''
抓取網(wǎng)頁文件內(nèi)容,保存到內(nèi)存
@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 文件內(nèi)容
'''
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)
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python實現(xiàn)爬取需要登錄的網(wǎng)站完整示例
這篇文章主要介紹了Python實現(xiàn)爬取需要登錄的網(wǎng)站,結(jié)合完整實例形式分析了Python登陸網(wǎng)站及數(shù)據(jù)抓取相關(guān)操作技巧,需要的朋友可以參考下2017-08-08
django自定義Field實現(xiàn)一個字段存儲以逗號分隔的字符串
這篇文章主要介紹了django自定義Field實現(xiàn)一個字段存儲以逗號分隔的字符串的示例,需要的朋友可以參考下2014-04-04
利用Python構(gòu)建Flutter應(yīng)用的教程詳解
Flutter在軟件研發(fā)領(lǐng)域是非常流行的,今天就讓我們深入了解一下,用?Python構(gòu)建flutter應(yīng)用程序的世界,感興趣的小伙伴可以跟隨小編一起了解一下2022-12-12
Pytorch加載數(shù)據(jù)集的方式總結(jié)及補充
Pytorch自定義數(shù)據(jù)集方法,應(yīng)該是用pytorch做算法的最基本的東西,下面這篇文章主要給大家介紹了關(guān)于Pytorch加載數(shù)據(jù)集的方式總結(jié)及補充,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2022-11-11

