使用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 相关文章推荐
多线程爬虫批量下载pcgame图片url 保存为xml的实现代码
Jan 17 Python
Python的__builtin__模块中的一些要点知识
May 02 Python
详解Django中的权限和组以及消息
Jul 23 Python
python中的字典操作及字典函数
Jan 03 Python
Python删除n行后的其他行方法
Jan 28 Python
python字典的setdefault的巧妙用法
Aug 07 Python
Python3多线程版TCP端口扫描器
Aug 31 Python
使用jupyter Nodebook查看函数或方法的参数以及使用情况
Apr 14 Python
pytorch查看通道数 维数 尺寸大小方式
May 26 Python
如何使用python写截屏小工具
Sep 29 Python
Python面向对象之成员相关知识总结
Jun 24 Python
Pandas 数据编码的十种方法
Apr 20 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
用cookies来跟踪识别用户
2006/10/09 PHP
php curl模拟post请求小实例
2013/11/13 PHP
php版微信公众平台实现预约提交后发送email的方法
2016/09/26 PHP
php中array_slice和array_splice函数解析
2016/10/18 PHP
php str_getcsv把字符串解析为数组的实现方法
2017/04/05 PHP
YII框架页面缓存操作示例
2019/04/29 PHP
驱动事件的addEvent.js代码
2007/03/27 Javascript
JavaScript 三种创建对象的方法
2009/10/16 Javascript
js 多浏览器分别判断代码
2010/04/01 Javascript
jQuery代码优化 遍历篇
2011/11/01 Javascript
从数组中随机取x条不重复数据的JS代码
2013/12/24 Javascript
jQuery 遍历函数详解
2015/07/05 Javascript
JavaScript实现简单获取当前网页网址的方法
2015/11/09 Javascript
深入探究AngularJS框架中Scope对象的超级教程
2016/01/04 Javascript
浅析node.js的模块加载机制
2018/05/25 Javascript
vue头部导航动态点击处理方法
2018/11/02 Javascript
微信小程序实现提交input信息到后台的方法示例
2019/01/19 Javascript
bootstrap-treeview实现多级树形菜单 后台JSON格式如何组织?
2019/07/26 Javascript
jquery实现轮播图特效
2020/04/12 jQuery
在vue中给后台接口传的值为数组的格式代码
2020/11/12 Javascript
Python greenlet实现原理和使用示例
2014/09/24 Python
Python自动化运维和部署项目工具Fabric使用实例
2016/09/18 Python
分析python切片原理和方法
2017/12/19 Python
浅析PHP与Python进行数据交互
2018/05/15 Python
django 将model转换为字典的方法示例
2018/10/16 Python
Python Matplotlib实现三维数据的散点图绘制
2019/03/19 Python
selenium中get_cookies()和add_cookie()的用法详解
2020/01/06 Python
Python json模块与jsonpath模块区别详解
2020/03/05 Python
python 获取剪切板内容的两种方法
2020/11/28 Python
Boston Proper官网:美国女装品牌
2017/10/30 全球购物
Redbubble法国:由独立艺术家设计的独特产品
2019/01/08 全球购物
导游的职业规划书范文
2013/12/27 职场文书
如何写一份好的英文求职信
2014/03/19 职场文书
2014年反腐倡廉工作总结
2014/12/05 职场文书
谢师宴家长答谢词
2015/09/30 职场文书
Tomcat安装使用及部署Web项目的3种方法汇总
2022/08/14 Servers