使用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链接Oracle数据库的方法
Jun 28 Python
python实现爬虫统计学校BBS男女比例(一)
Dec 31 Python
Centos Python2 升级到Python3的简单实现
Jun 21 Python
Python自定义简单图轴简单实例
Jan 08 Python
对Python 多线程统计所有csv文件的行数方法详解
Feb 12 Python
Python字符串逆序输出的实例讲解
Feb 16 Python
Python猴子补丁知识点总结
Jan 05 Python
python集合删除多种方法详解
Feb 10 Python
如何向scrapy中的spider传递参数的几种方法
Nov 18 Python
Python制作表白爱心合集
Jan 22 Python
python 使用tkinter与messagebox写界面和弹窗
Mar 20 Python
pandas时间序列之pd.to_datetime()的实现
Jun 16 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中使用Ajax时出现Error(c00ce56e)的详细解决方案
2014/11/03 PHP
linux中cd命令使用详解
2015/01/08 PHP
PHP几个实用自定义函数小结
2016/01/25 PHP
Javascript 倒计时源代码.(时.分.秒) 详细注释版
2011/05/09 Javascript
jQuery中trigger()方法用法实例
2015/01/19 Javascript
javascript表格隔行变色加鼠标移入移出及点击效果的方法
2015/04/10 Javascript
JavaScript转换二进制编码为ASCII码的方法
2015/04/16 Javascript
使用postMesssage()实现跨域iframe页面间的信息传递方法
2016/03/29 Javascript
基于jQuery插件实现点击小图显示大图效果
2016/05/11 Javascript
详解用vue-cli来搭建vue项目和webpack
2017/04/20 Javascript
强大的 Angular 表单验证功能详细介绍
2017/05/23 Javascript
详解react-router如何实现按需加载
2017/06/15 Javascript
requirejs + vue 项目搭建详解
2017/06/16 Javascript
在React 组件中使用Echarts的示例代码
2017/11/08 Javascript
Python中index()和seek()的用法(详解)
2017/04/27 Python
Python内置函数 next的具体使用方法
2017/11/24 Python
win10下tensorflow和matplotlib安装教程
2018/09/19 Python
详解分布式任务队列Celery使用说明
2018/11/29 Python
python rolling regression. 使用 Python 实现滚动回归操作
2020/06/08 Python
英国时尚和家居用品零售商:Matalan
2021/02/28 全球购物
初中物理教学反思
2014/01/14 职场文书
人民调解员先进事迹材料
2014/05/08 职场文书
遵纪守法演讲稿
2014/05/23 职场文书
承诺书范文
2014/06/03 职场文书
租房协议书样本
2014/08/20 职场文书
群众路线教育实践活动心得体会(教师)
2014/10/31 职场文书
2014年节能工作总结
2014/12/18 职场文书
市场总监岗位职责
2015/02/11 职场文书
2015年七夕情人节活动方案
2015/05/06 职场文书
2015年中学校长工作总结
2015/05/19 职场文书
新郎父亲婚礼致辞
2015/07/27 职场文书
2016年重阳节慰问信
2015/12/01 职场文书
Golang中interface{}转为数组的操作
2021/04/30 Golang
SQLServer之常用函数总结详解
2021/08/30 SQL Server
《仙剑客栈2》第一弹正式宣传片公开 年内发售
2022/04/07 其他游戏
Windows Server 2012 R2服务器安装与配置的完整步骤
2022/07/15 Servers