python selenium抓取页面截图+连接oracle数据库 博客分类: python
程序员文章站
2024-03-24 15:23:04
...
最近有需求抓取自己系统页面截图并作为邮件正文发送,经评估 发现python做这事儿最适合不过:
抓图依赖 python脚本+FIREFOX
准备:
1.数据库建立抓取配置表,存页面访问路径、文件存储路径
2.安装所需的 py包 :seleniu的安装网上很多教程;下载对应的数据库驱动
代码实现如下:
软件版本:Firefox47 cx-Oracle (5.2.1) selenium (2.53.6)
# -*- coding: utf-8 -*- # author lzy from selenium import webdriver import time,datetime import cx_Oracle def capture(url, pdir): browser = webdriver.Firefox() # Get local session of firefox browser.set_window_size(600, 900) browser.get(url) # Load page browser.execute_script(""" (function () { var y = 0; var step = 100; window.scroll(0, 0); function f() { if (y < document.body.scrollHeight) { y += step; window.scroll(0, y); setTimeout(f, 50); } else { window.scroll(0, 0); document.title += "scroll-done"; } } setTimeout(f, 1000); })(); """) for i in xrange(30): if "scroll-done" in browser.title: break time.sleep(1) vdt = (datetime.datetime.now() - datetime.timedelta(days = 1)).strftime("%Y%m%d") browser.save_screenshot('D:/mywork/image/%s/screenShots_%s.png'%(pdir,vdt)) browser.close() if __name__ == "__main__": conn = cx_Oracle.connect('username','password','tnsname') cursor=conn.cursor(); sql='select f_sendurl,f_reportname from table where f_deflag = 0' cursor.execute(sql) rows =cursor.fetchall() for row in rows: capture(row[0],row[1]) cursor.close() conn.close()