码迷,mamicode.com
首页 > Web开发 > 详细

找到网页的某一区域,截图,并保存

时间:2017-03-21 15:45:30      阅读:520      评论:0      收藏:0      [点我收藏+]

标签:gets   size   ntb   python   exe   direct   alt   style   ref   

python 实现


from
PIL import Image from selenium import webdriver import time; # 引入time模块 def webscreen(i): print(i) if i == 1 or i == 700: print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())) # url = ‘http://192.168.20.58/gfwd/comments-1.html‘ url = http://192.168.20.58/gfwd/comments- + str(i) + .html # driver = webdriver.Chrome("C:/Users/maimai/Downloads/chromedriver_win32/chromedriver.exe") # driver = webdriver.PhantomJS("?D:/迅雷下载/phantomjs-2.1.1-windows/bin/phantomjs.exe") driver = webdriver.PhantomJS(r"D:\迅雷下载\phantomjs-2.1.1-windows\bin\phantomjs.exe") # ?D:\迅雷下载\phantomjs-2.1.1-windows\bin\phantomjs.exe driver.set_page_load_timeout(300) driver.set_window_size(1280, 800) driver.get(url) imgelement = driver.find_element_by_id(screen) location = imgelement.location size = imgelement.size savepath = rimages/codingpy_ + str(i) + .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)

 c#实现

 static void Main(string[] args)
        {
            var url = "http://192.168.20.58/gfwd/comments-1.html";
            using (var driver = new PhantomJSDriver(@"E:\selenium\phantomjs-2.1.1-windows\bin\"))
            {
                //进入百度首页
                driver.Navigate().GoToUrl(url);
                driver.Manage().Window.Size = new Size(1280, 800);

                //设置窗体最大化
                //driver.Manage().Window.Maximize();
                //找到对象
                var imgelement = driver.FindElementById("screen");
                var location = imgelement.Location;
                var size = imgelement.Size;

                var savepath = Environment.CurrentDirectory + "\\codingpy_1.png";
                driver.GetScreenshot().SaveAsFile(savepath, ScreenshotImageFormat.Png);//屏幕截图   
                Image image = System.Drawing.Image.FromFile(savepath);
                int left = location.X;
                int top = location.Y;
                int right = left + size.Width;
                int bottom = top + size.Height;
                //截图
                Bitmap bitmap = new Bitmap(savepath);//原图
                Bitmap destBitmap = new Bitmap(size.Width, size.Height);//目标图
                Rectangle destRect = new Rectangle(0, 0, size.Width, size.Height);//矩形容器
                Rectangle srcRect = new Rectangle(left, top, size.Width, size.Height);
                Graphics graphics = Graphics.FromImage(destBitmap);
                graphics.DrawImage(bitmap, destRect, srcRect, GraphicsUnit.Pixel);
                destBitmap.Save("d:\\aa.Png");
                graphics.Dispose();
            }
        }

技术分享

 

 

 

参考链接 C#对图片文件的压缩、裁剪操作初探 http://www.cnblogs.com/xyang/archive/2013/02/25/2932145.html

            

自动化测试:Selenium webdriver 学习笔记-C#版(二)

http://seleniumhq.github.io/selenium/docs/api/dotnet/index.html api文档

找到网页的某一区域,截图,并保存

标签:gets   size   ntb   python   exe   direct   alt   style   ref   

原文地址:http://www.cnblogs.com/baicaocanhua/p/6594172.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!