使用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 相关文章推荐
Python常用的日期时间处理方法示例
Feb 08 Python
Python中列表和元组的相关语句和方法讲解
Aug 20 Python
让Python更加充分的使用Sqlite3
Dec 11 Python
使用pandas的DataFrame的plot方法绘制图像的实例
May 24 Python
python3安装speech语音模块的方法
Dec 24 Python
在python中使用requests 模拟浏览器发送请求数据的方法
Dec 26 Python
python-itchat 获取微信群用户信息的实例
Feb 21 Python
python输入错误后删除的方法
Oct 12 Python
python快速排序的实现及运行时间比较
Nov 22 Python
python Shapely使用指南详解
Feb 18 Python
Python可变对象与不可变对象原理解析
Feb 25 Python
Python实现钉钉/企业微信自动打卡的示例代码
Feb 02 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
我常用的几个类
2006/10/09 PHP
DedeCms模板安装/制作概述
2007/03/11 PHP
php 删除cookie和浏览器重定向
2009/03/16 PHP
解析thinkphp中的导入文件标签
2013/06/20 PHP
实例简介PHP的一些高级面向对象编程的特性
2015/11/27 PHP
使用Huagepage和PGO来提升PHP7的执行性能
2015/11/30 PHP
PHP数组函数array_multisort()用法实例分析
2016/04/02 PHP
PHP实现微信小程序用户授权的工具类示例
2019/03/05 PHP
可以把编码转换成 gb2312编码lib.UTF8toGB2312.js
2007/08/21 Javascript
Prototype Class对象学习
2009/07/19 Javascript
javascript简单实现表格行间隔显示颜色并高亮显示
2013/11/29 Javascript
JavaScript 性能优化小结
2015/10/12 Javascript
angularJS之$http:与服务器交互示例
2017/03/17 Javascript
详解VUE单页应用骨架屏方案
2019/01/17 Javascript
vue slot与传参实例代码讲解
2019/04/28 Javascript
koa中间件核心(koa-compose)源码解读分析
2020/06/15 Javascript
vue实现表格合并功能
2020/12/01 Vue.js
[04:48]DOTA2亚洲邀请赛林书豪为VGJ加油
2017/04/01 DOTA
[00:29]2019完美世界全国高校联赛(秋季赛)总决赛海口落幕
2019/12/10 DOTA
Python处理文本文件中控制字符的方法
2017/02/07 Python
Python MD5加密实例详解
2017/08/02 Python
Python面向对象基础入门之编码细节与注意事项
2018/12/11 Python
Django实现一对多表模型的跨表查询方法
2018/12/18 Python
python随机在一张图像上截取任意大小图片的方法
2019/01/24 Python
详解python中自定义超时异常的几种方法
2019/07/29 Python
Python decimal模块使用方法详解
2020/06/08 Python
详解Pycharm与anaconda安装配置指南
2020/08/25 Python
CSS3 伪类选择器 nth-child()说明
2010/07/10 HTML / CSS
韩国美国时尚服装和美容在线全球市场:KOODING
2018/11/07 全球购物
名词解释WEB SERVICE,SOAP,UDDI,WSDL,JAXP,JAXM;JSWDL开发包的介绍。
2012/10/27 面试题
教师师德承诺书
2014/03/26 职场文书
房产代理公证处委托书
2014/04/04 职场文书
关于随地扔垃圾的检讨书
2014/09/30 职场文书
2016入党积极分子考察评语
2015/12/01 职场文书
《假如》教学反思
2016/02/17 职场文书
深入浅析python3 依赖倒置原则(示例代码)
2021/07/09 Python