使用Python保存网页上的图片或者保存页面为截图


Posted in Python onMarch 05, 2016

Python保存网页图片
这个是个比较简单的例子,网页中的图片地址都是使用'http://。。。。.jpg'这种方式直接定义的。

使用前,可以先建立好一个文件夹用于保存图片,本例子中使用的文件夹是 d:\\pythonPath这个文件夹

代码如下:

# -*- coding: UTF-8 -*- 
import os,re,urllib,uuid 
 
#首先定义云端的网页,以及本地保存的文件夹地址 
urlPath='http://gamebar.com/' 
localPath='d:\\pythonPath' 
 
 
#从一个网页url中获取图片的地址,保存在 
#一个list中返回 
def getUrlList(urlParam): 
  urlStream=urllib.urlopen(urlParam) 
  htmlString=urlStream.read() 
  if( len(htmlString)!=0 ): 
    patternString=r'http://.{0,50}\.jpg' 
    searchPattern=re.compile(patternString) 
    imgUrlList=searchPattern.findall(htmlString) 
    return imgUrlList 
 
     
#生成一个文件名字符串  
def generateFileName(): 
  return str(uuid.uuid1()) 
 
   
#根据文件名创建文件  
def createFileWithFileName(localPathParam,fileName): 
  totalPath=localPathParam+'\\'+fileName 
  if not os.path.exists(totalPath): 
    file=open(totalPath,'a+') 
    file.close() 
    return totalPath 
   
 
#根据图片的地址,下载图片并保存在本地  
def getAndSaveImg(imgUrl): 
  if( len(imgUrl)!= 0 ): 
    fileName=generateFileName()+'.jpg' 
    urllib.urlretrieve(imgUrl,createFileWithFileName(localPath,fileName)) 
 
 
#下载函数 
def downloadImg(url): 
  urlList=getUrlList(url) 
  for urlString in urlList: 
    getAndSaveImg(urlString) 
     
downloadImg(urlPath)

保存的文件如下:

使用Python保存网页上的图片或者保存页面为截图

网页的一部分保存为图片
主要思路是selenium+phantomjs(中文网页需要设置字体)+PIL切图

def webscreen():
  url = 'http://www.xxx.com'
  driver = webdriver.PhantomJS()
  driver.set_page_load_timeout(300)
  driver.set_window_size(1280,800)
  driver.get(url)
  imgelement = driver.find_element_by_id('XXXX')
  location = imgelement.location
  size = imgelement.size
  savepath = r'XXXX.png'
  driver.save_screenshot(savepath)
  im = Image.open(savepath)
  left = location['x']
  top = location['y']
  right = left + size['width']
  bottom = location['y'] + size['height']
  im = im.crop((left,top,right,bottom))
  im.save(savepath)
Python 相关文章推荐
天翼开放平台免费短信验证码接口使用实例
Dec 18 Python
Python判断文件或文件夹是否存在的三种方法
Jul 27 Python
python如何将图片转换为字符图片
Aug 19 Python
PyTorch上实现卷积神经网络CNN的方法
Apr 28 Python
python抓取网站的图片并下载到本地的方法
May 22 Python
Python设计模式之职责链模式原理与用法实例分析
Jan 11 Python
解决python测试opencv时imread导致的错误问题
Jan 26 Python
python实现四人制扑克牌游戏
Apr 22 Python
基于python3.7利用Motor来异步读写Mongodb提高效率(推荐)
Apr 29 Python
matplotlib绘制多子图共享鼠标光标的方法示例
Jan 08 Python
python自动化办公操作PPT的实现
Feb 05 Python
Python实现简单的猜单词
Jun 15 Python
Python发送form-data请求及拼接form-data内容的方法
Mar 05 #Python
Python多线程爬虫简单示例
Mar 04 #Python
使用Python来开发Markdown脚本扩展的实例分享
Mar 04 #Python
使用py2exe在Windows下将Python程序转为exe文件
Mar 04 #Python
用Python编写简单的微博爬虫
Mar 04 #Python
python相似模块用例
Mar 04 #Python
Python程序中用csv模块来操作csv文件的基本使用教程
Mar 03 #Python
You might like
php简单实现短网址(短链)还原的方法(测试可用)
2016/05/09 PHP
thinkphp5.0整合phpsocketio完整攻略(绕坑)
2018/10/12 PHP
JS Timing
2007/04/21 Javascript
jquery选择器之内容过滤选择器详解
2014/01/27 Javascript
JavaScript中实现继承的三种方式和实例
2015/01/29 Javascript
JS中动态创建元素的三种方法总结(推荐)
2016/10/20 Javascript
解决Window10系统下Node安装报错的问题分析
2016/12/13 Javascript
详解Angular2表单-模板驱动的表单(Template-Driven Forms)
2017/08/04 Javascript
详解vuex中mapState,mapGetters,mapMutations,mapActions的作用
2018/04/13 Javascript
js实现拖动缓动效果
2020/01/13 Javascript
vue-cli设置css不生效的解决方法
2020/02/07 Javascript
基于Vue实现微前端的示例代码
2020/04/24 Javascript
[02:23]DOTA2英雄基础教程 幻影长矛手
2013/12/09 DOTA
[03:22]DAC最前线(第二期)—DOTA2亚洲邀请赛主赛场周边及线路探访
2015/01/24 DOTA
python进阶教程之函数参数的多种传递方法
2014/08/30 Python
Python可变参数函数用法实例
2015/07/07 Python
老生常谈Python序列化和反序列化
2017/06/28 Python
Python抓取框架Scrapy爬虫入门:页面提取
2017/12/01 Python
python+opencv实现阈值分割
2018/12/26 Python
Python3中lambda表达式与函数式编程讲解
2019/01/14 Python
对python以16进制打印字节数组的方法详解
2019/01/24 Python
python3安装crypto出错及解决方法
2019/07/30 Python
详解Python self 参数
2019/08/30 Python
python实现猜数字游戏
2020/03/25 Python
python中如何写类
2020/06/29 Python
HTML5中5个简单实用的API
2014/04/28 HTML / CSS
太阳镜仓库,售价20美元或更少:Sunglass Warehouse
2016/09/28 全球购物
英国厨房与餐具用品为主的设计品牌:Joseph Joseph
2018/04/26 全球购物
解释一下ArrayList Vector和LinkedList的实现和区别
2013/04/26 面试题
建筑专业自荐信
2013/10/18 职场文书
学校门卫岗位职责
2014/03/16 职场文书
学校食堂标语
2014/10/06 职场文书
入股协议书范本
2014/11/01 职场文书
人事主管岗位职责
2015/02/04 职场文书
给老师的保证书怎么写
2015/05/09 职场文书
毕业答辩开场白范文
2015/05/27 职场文书