利用Selenium+PhantomJS 实现截图

Posted yjq_叶

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了利用Selenium+PhantomJS 实现截图相关的知识,希望对你有一定的参考价值。

using OpenQA.Selenium;
using OpenQA.Selenium.PhantomJS;
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Threading.Tasks;

namespace WebKitTest
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            Parallel.For(0, 20, i =>
            {
                var url = "http://www.baidu.com";
                IWebDriver driver = new PhantomJSDriver(GetPhantomJSDriverService());
                driver.Navigate().GoToUrl(url);
                ITakesScreenshot screenshotDriver = driver as ITakesScreenshot;
                Screenshot screenshot = screenshotDriver.GetScreenshot();
                using (MemoryStream stream = new MemoryStream(screenshot.AsByteArray))
                {
                    using (var soourceImage = Image.FromStream(stream))
                    {
                        var cutedImage = GetReducedImage(800, 800, soourceImage);
                        if (cutedImage != null)
                        {
                            cutedImage.Save(AppDomain.CurrentDomain.BaseDirectory + "//" + i.ToString() + ".jpg", ImageFormat.Jpeg);
                        }
                    }
                }
            });
            for (int i = 0; i < 100; i++)
            {
            }
            Console.Read();
        }

        private static PhantomJSDriverService GetPhantomJSDriverService()
        {
            PhantomJSDriverService pds = PhantomJSDriverService.CreateDefaultService();
            //设置代理服务器地址
            //pds.Proxy = $"{ip}:{port}";
            //设置代理服务器认证信息
            //pds.ProxyAuthentication = GetProxyAuthorization();
            return pds;
        }

        /// <summary>
        /// 生成缩略图重载方法1,返回缩略图的Image对象
        /// </summary>
        /// <param name="targetWidth">缩略图的宽度</param>
        /// <param name="targetHeight">缩略图的高度</param>
        /// <returns>缩略图的Image对象</returns>
        public static Image GetReducedImage(int targetWidth, int targetHeight, Image sourceImage)
        {
            try
            {
                //用指定的大小和格式初始化Bitmap类的新实例
                Bitmap bitmap = new Bitmap(targetWidth, targetHeight, PixelFormat.Format32bppArgb);
                //从指定的Image对象创建新Graphics对象
                using (Graphics graphics = Graphics.FromImage(bitmap))
                {
                    //清除整个绘图面并以透明背景色填充
                    graphics.Clear(Color.Transparent);
                    //在指定位置并且按指定大小绘制原图片对象

                    int sourceWidth = sourceImage.Width;
                    int sourceHeight = sourceImage.Height;
                    int sourceStartX = 0;
                    int targetStartX = 0;
                    int sourceStartY = 0;
                    int targetStartY = 0;
                    int sourceEndX = 0;
                    int targetEndX = 0;
                    int sourceEndY = 0;
                    int targetEndY = 0;
                    if (sourceWidth <= targetWidth)
                    {
                        sourceStartX = 0;
                        targetStartX = (targetWidth - sourceWidth) / 2;
                        sourceEndX = sourceWidth;
                        targetEndX = sourceWidth + targetStartX;
                    }
                    else
                    {
                        sourceStartX = (sourceWidth - targetWidth) / 2;
                        targetStartX = 0;
                        sourceEndX = targetWidth + sourceStartX;
                        targetEndX = targetWidth;
                    }
                    if (targetEndX - sourceStartX > targetWidth)
                    {
                        targetEndX = targetWidth - sourceStartX;
                    }

                    if (sourceHeight <= targetHeight)
                    {
                        sourceEndY = sourceHeight;
                        targetEndY = sourceHeight;
                    }
                    else
                    {
                        sourceEndY = targetHeight;
                        targetEndY = targetHeight;
                    }
                    Rectangle destRect = new Rectangle(targetStartX, targetStartY, targetEndX - targetStartX, targetEndY - targetStartY);//矩形容器
                    Rectangle srcRect = new Rectangle(sourceStartX, sourceStartY, sourceEndX - sourceStartX, sourceEndY - sourceStartY);

                    graphics.DrawImage(sourceImage, destRect, srcRect, GraphicsUnit.Pixel);
                    return bitmap;
                }
            }
            catch (Exception e)
            {
                return null;
            }
        }
    }
}

 

        private static void Main(string[] args)        {            Parallel.For(0, 20, i =>            {                var url = "http://www.baidu.com";                IWebDriver driver = new PhantomJSDriver(GetPhantomJSDriverService());                driver.Navigate().GoToUrl(url);                ITakesScreenshot screenshotDriver = driver as ITakesScreenshot;                Screenshot screenshot = screenshotDriver.GetScreenshot();                using (MemoryStream stream = new MemoryStream(screenshot.AsByteArray))                {                    using (var soourceImage = Image.FromStream(stream))                    {                        var cutedImage = GetReducedImage(800, 800, soourceImage);                        if (cutedImage != null)                        {                            cutedImage.Save(AppDomain.CurrentDomain.BaseDirectory + "//" + i.ToString() + ".jpg", ImageFormat.Jpeg);                        }                    }                }            });            for (int i = 0; i < 100; i++)            {            }            Console.Read();        }
        private static PhantomJSDriverService GetPhantomJSDriverService()        {            PhantomJSDriverService pds = PhantomJSDriverService.CreateDefaultService();            //设置代理服务器地址            //pds.Proxy = $"{ip}:{port}";            //设置代理服务器认证信息            //pds.ProxyAuthentication = GetProxyAuthorization();            return pds;        }
        /// <summary>        /// 生成缩略图重载方法1,返回缩略图的Image对象        /// </summary>        /// <param name="targetWidth">缩略图的宽度</param>        /// <param name="targetHeight">缩略图的高度</param>        /// <returns>缩略图的Image对象</returns>        public static Image GetReducedImage(int targetWidth, int targetHeight, Image sourceImage)        {            try            {                //用指定的大小和格式初始化Bitmap类的新实例                Bitmap bitmap = new Bitmap(targetWidth, targetHeight, PixelFormat.Format32bppArgb);                //从指定的Image对象创建新Graphics对象                using (Graphics graphics = Graphics.FromImage(bitmap))                {                    //清除整个绘图面并以透明背景色填充                    graphics.Clear(Color.Transparent);                    //在指定位置并且按指定大小绘制原图片对象
                    int sourceWidth = sourceImage.Width;                    int sourceHeight = sourceImage.Height;                    int sourceStartX = 0;                    int targetStartX = 0;                    int sourceStartY = 0;                    int targetStartY = 0;                    int sourceEndX = 0;                    int targetEndX = 0;                    int sourceEndY = 0;                    int targetEndY = 0;                    if (sourceWidth <= targetWidth)                    {                        sourceStartX = 0;                        targetStartX = (targetWidth - sourceWidth) / 2;                        sourceEndX = sourceWidth;                        targetEndX = sourceWidth + targetStartX;                    }                    else                    {                        sourceStartX = (sourceWidth - targetWidth) / 2;                        targetStartX = 0;                        sourceEndX = targetWidth + sourceStartX;                        targetEndX = targetWidth;                    }                    if (targetEndX - sourceStartX > targetWidth)                    {                        targetEndX = targetWidth - sourceStartX;                    }
                    if (sourceHeight <= targetHeight)                    {                        sourceEndY = sourceHeight;                        targetEndY = sourceHeight;                    }                    else                    {                        sourceEndY = targetHeight;                        targetEndY = targetHeight;                    }                    Rectangle destRect = new Rectangle(targetStartX, targetStartY, targetEndX - targetStartX, targetEndY - targetStartY);//矩形容器                    Rectangle srcRect = new Rectangle(sourceStartX, sourceStartY, sourceEndX - sourceStartX, sourceEndY - sourceStartY);
                    graphics.DrawImage(sourceImage, destRect, srcRect, GraphicsUnit.Pixel);                    return bitmap;                }            }            catch (Exception e)            {                return null;            }        }

以上是关于利用Selenium+PhantomJS 实现截图的主要内容,如果未能解决你的问题,请参考以下文章

Windows下 Python Selenium PhantomJS 抓取网页并截图

Selenium PhantomJS webdriver获得部分截图

[Python爬虫] 之十九:Selenium +phantomjs 利用 pyquery抓取超级TV网数据

[Python爬虫] 之二十六:Selenium +phantomjs 利用 pyquery抓取智能电视网站图片信息

利用PIL和Selenium实现页面元素截图

使用selenium和phantomJS浏览器登陆豆瓣的小演示