使用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使用multiprocessing实现一个最简单的分布式作业调度系统
Mar 14 Python
基于Python实现的ID3决策树功能示例
Jan 02 Python
Django使用httpresponse返回用户头像实例代码
Jan 26 Python
Django框架教程之正则表达式URL误区详解
Jan 28 Python
Python代码缩进和测试模块示例详解
May 07 Python
pandas把所有大于0的数设置为1的方法
Jan 26 Python
python中多个装饰器的调用顺序详解
Jul 16 Python
解决Python设置函数调用超时,进程卡住的问题
Aug 08 Python
pytorch标签转onehot形式实例
Jan 02 Python
详解使用Python写一个向数据库填充数据的小工具(推荐)
Sep 11 Python
Pycharm自带Git实现版本管理的方法步骤
Sep 18 Python
python使用matplotlib:subplot绘制多个子图的示例
Sep 24 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 CURL用法的深入分析
2013/06/09 PHP
PHP中source #N问题的解决方法
2014/01/27 PHP
浅析php设计模式之数据对象映射模式
2016/03/03 PHP
Laravel中使用FormRequest进行表单验证方法及问题汇总
2016/06/19 PHP
php微信扫码支付 php公众号支付
2019/03/24 PHP
PHP时间类完整代码实例
2021/02/26 PHP
js检测浏览器版本、核心、是否移动端示例
2014/04/24 Javascript
Bootstrap入门书籍之(零)Bootstrap简介
2016/02/17 Javascript
JS中页面与页面之间超链接跳转中文乱码问题的解决办法
2016/12/15 Javascript
JSON字符串和JSON对象相互转化实例详解
2017/01/05 Javascript
boostrapTable的refresh和refreshOptions区别浅析
2017/01/22 Javascript
jQuery插件ImgAreaSelect实现头像上传预览和裁剪功能实例讲解一
2017/05/26 jQuery
详解vue 组件之间使用eventbus传值
2017/10/25 Javascript
[01:14]辉夜杯战队访谈宣传片—NEWBEE.Y
2015/12/26 DOTA
[01:07:47]Secret vs Optic Supermajor 胜者组 BO3 第一场 6.4
2018/06/05 DOTA
Python 实现一行输入多个值的方法
2018/04/21 Python
Python3爬虫之urllib携带cookie爬取网页的方法
2018/12/28 Python
Python实现简单的列表冒泡排序和反转列表操作示例
2019/07/10 Python
Python tornado上传文件的功能
2020/03/26 Python
selenium与xpath之获取指定位置的元素的实现
2021/01/26 Python
python 实现有道翻译功能
2021/02/26 Python
css3.0 图形构成实例练习二
2013/03/19 HTML / CSS
基于CSS3特效之动画:animation的应用
2013/05/09 HTML / CSS
微信小程序“圣诞帽”的实现思路详解
2017/12/28 HTML / CSS
详解h5页面在不同ios设备上的问题总结
2019/03/01 HTML / CSS
高山背包:High Sierra
2017/11/23 全球购物
新东方旗下远程教育网站:新东方在线
2020/03/19 全球购物
入党积极分子介绍信
2014/01/17 职场文书
最新奶茶店创业计划书
2014/01/25 职场文书
2014端午节活动策划方案
2014/01/27 职场文书
1000字打架检讨书
2014/11/03 职场文书
2015年挂职锻炼工作总结
2014/12/12 职场文书
优秀大学生自荐信
2015/03/26 职场文书
[有人@你]你有一封绿色倡议书,请查收!
2019/07/18 职场文书
python实现简易自习室座位预约系统
2021/06/30 Python
postman中form-data、x-www-form-urlencoded、raw、binary的区别介绍
2022/01/18 HTML / CSS