今天就跟大家聊聊有關使用python怎么保存網(wǎng)頁圖片到本地,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據(jù)這篇文章可以有所收獲。

具體內容如下
#!/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)頁文件內容,保存到內存
@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__":
h2 = GetImage()
#h2.save_file('c:/log/124.png',h2.get_file('https://cache.yisu.com/upload/information/20200622/113/49277'))
#url = 'https://cache.yisu.com/upload/information/20200622/113/49277'
#file_path ='c:/log/125.png'
#h2.save_png_file(file_path,url)python的五大特點是什么python的五大特點:1.簡單易學,開發(fā)程序時,專注的是解決問題,而不是搞明白語言本身。2.面向對象,與其他主要的語言如C++和Java相比, Python以一種非常強大又簡單的方式實現(xiàn)面向對象編程。3.可移植性,Python程序無需修改就可以在各種平臺上運行。4.解釋性,Python語言寫的程序不需要編譯成二進制代碼,可以直接從源代碼運行程序。5.開源,Python是 FLOSS(自由/開放源碼軟件)之一。
看完上述內容,你們對使用python怎么保存網(wǎng)頁圖片到本地有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。
本文題目:使用python怎么保存網(wǎng)頁圖片到本地-創(chuàng)新互聯(lián)
分享地址:http://www.chinadenli.net/article20/dpchjo.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供響應式網(wǎng)站、網(wǎng)站改版、定制開發(fā)、外貿網(wǎng)站建設、品牌網(wǎng)站設計、App設計
聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內容