python URL模块实例

来源:互联网 发布:桶装水管理软件 编辑:程序博客网 时间:2024/06/04 20:08

再进行python编码过程中,需要对网站上图片进行下载保存到本地,以下实例实现将指定的图片下载保存到本地:

#!/usr/bin/python# -*- coding: UTF-8 -*-'''@author:freesigefeiCreated on 2016年3月11日'''#本模块作用为通过直接访问图片下载地址,直接将图片下载保存到本地import os,urllib#首先定义图片下载的网址,以及图片本地保存的文件夹地址 urlPath='http://192.168.1.1/logo'      #根据文件名创建文件  def createFileWithFileName(localPathParam,fileName):     '''根据文件名创建文件,localPathParam保存的本地路径,fileName保存的文件名'''    totalPath=localPathParam+fileName    if not os.path.exists(totalPath):         Image_file=open(totalPath,'a+')         Image_file.close()     return totalPath #根据图片的地址,下载图片并保存在本地  def getAndSaveImg(imgUrl,fileName):     '''将下载的图片保存到本地,imgUrl表示下载地址,fileName表示保存的文件名'''    localPath='D:\\download\\' #localPath表示保存的本地路径    if( len(imgUrl)!= 0 ):        urllib.urlretrieve(imgUrl,createFileWithFileName(localPath,fileName))if __name__ == '__main__':        getAndSaveImg(urlPath,'Logo.jpg')
通过运行本实例,可以将对应地址的logo图片下载到本地目录下

0 0
原创粉丝点击