使用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 的类、继承和多态详解
Jul 16 Python
浅谈Django中的数据库模型类-models.py(一对一的关系)
May 30 Python
基于python实现聊天室程序
Jul 27 Python
解决pyinstaller打包exe文件出现命令窗口一闪而过的问题
Oct 31 Python
Python 单元测试(unittest)的使用小结
Nov 14 Python
python 将字符串中的数字相加求和的实现
Jul 18 Python
Python 实现的 Google 批量翻译功能
Aug 26 Python
python将字典列表导出为Excel文件的方法
Sep 02 Python
python快速排序的实现及运行时间比较
Nov 22 Python
python如何构建mock接口服务
Jan 28 Python
python异步的ASGI与Fast Api实现
Jul 16 Python
利用python实时刷新基金估值(摸鱼小工具)
Sep 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将向Java靠拢
2006/10/09 PHP
php db类库进行数据库操作
2009/03/19 PHP
php4与php5的区别小结(配置异同)
2011/12/20 PHP
php使用Cookie实现和用户会话的方法
2015/01/21 PHP
jQuery实现的类flash菜单效果代码
2010/05/17 Javascript
创建js对象和js类的方法汇总
2014/12/24 Javascript
AJAX实现瀑布流触发分页与分页触发瀑布流的方法
2016/05/23 Javascript
jQuery+Ajax+PHP弹出层异步登录效果(附源码下载)
2016/05/27 Javascript
jQuery简单实现iframe的高度根据页面内容自适应的方法
2016/08/01 Javascript
用jquery快速解决IE输入框不能输入的问题
2016/10/04 Javascript
Bootstrap modal只加载一次数据的解决办法(推荐)
2017/11/24 Javascript
vue打包之后生成一个配置文件修改接口的方法
2018/12/09 Javascript
深入探讨JavaScript的最基本部分之执行上下文
2019/02/12 Javascript
python执行等待程序直到第二天零点的方法
2015/04/23 Python
Python中函数eval和ast.literal_eval的区别详解
2017/08/10 Python
Python编写Windows Service服务程序
2018/01/04 Python
对pandas进行数据预处理的实例讲解
2018/04/20 Python
python3.6使用pickle序列化class的方法
2018/10/22 Python
Python3爬虫全国地址信息
2019/01/05 Python
浅谈PySpark SQL 相关知识介绍
2019/06/14 Python
使用python快速实现不同机器间文件夹共享方式
2019/12/22 Python
Python 写了个新型冠状病毒疫情传播模拟程序
2020/02/14 Python
python GUI库图形界面开发之PyQt5不规则窗口实现与显示GIF动画的详细方法与实例
2020/03/09 Python
python3发送request请求及查看返回结果实例
2020/04/30 Python
Spark处理数据排序问题如何避免OOM
2020/05/21 Python
python 数据类型强制转换的总结
2021/01/25 Python
Html5 video标签视频的最佳实践
2020/02/26 HTML / CSS
巴西独家产品和现场演示购物网站:Shoptime
2019/07/11 全球购物
护士自我鉴定范文
2013/10/06 职场文书
高中军训感言400字
2014/02/24 职场文书
酒店辞职信怎么写
2015/02/27 职场文书
工程催款通知书
2015/04/17 职场文书
毕业证明书
2015/06/19 职场文书
2015年中学团委工作总结
2015/07/22 职场文书
2016年暑期教师培训心得体会
2016/01/09 职场文书
Mysql 数据库中的 redo log 和 binlog 写入策略
2022/04/26 MySQL