欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java操作使用phantomjs网页转换图片

程序员文章站 2024-03-25 14:04:58
...

安装phantomjs

链接: 安装phantomjs.

依赖

        <!--phantomjs -->
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>2.53.1</version>
        </dependency>
        <dependency>
            <groupId>com.github.detro</groupId>
            <artifactId>ghostdriver</artifactId>
            <version>2.1.0</version>
        </dependency>

遇到冲突情况下swagger依赖来冲突
其他暂时未遇到

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.7.0</version>
            <exclusions>
                <exclusion>
                    <groupId>com.google.guava</groupId>
                    <artifactId>guava</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.7.0</version>
            <exclusions>
                <exclusion>
                    <groupId>com.google.guava</groupId>
                    <artifactId>guava</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

运行代码


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriverService;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;


@Api(value = "ImplementsCmd")
@RestController("/PhantomJs")
public class ImplementsCmd {

	@ApiOperation(value = "网页转图片", nickname = "PhantomJsByImage")
	@GetMapping("imageByUrl")
	public void PhantomJsByImage(@RequestParam("url") String url,@RequestParam("date") String date) throws InterruptedException, IOException {
		try {
			if(1 > Integer.valueOf(date) || Integer.valueOf(date) > 10){
				return;
			}
		}catch (Exception e){
			return;
		}

		//设置必要参数
		DesiredCapabilities dcaps = new DesiredCapabilities();
		//ssl证书支持
		dcaps.setCapability("acceptSslCerts", true);
		//截屏支持
		dcaps.setCapability("takesScreenshot", true);
		//css搜索支持
		dcaps.setCapability("cssSelectorsEnabled", true);
		//js支持
		dcaps.setJavascriptEnabled(true);
		//驱动支持(第二参数表明的是你的phantomjs引擎所在的路径)
//		dcaps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,
//				"/root/data/phantomjs/phantomjs/bin/phantomjs");//linux
		dcaps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,
				"D:\\APP\\phantomjs\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe");
		//创建*面浏览器对象
		PhantomJSDriver driver = new PhantomJSDriver(dcaps);
		//设置隐性等待(作用于全局)
		driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
		long start = System.currentTimeMillis();
		//打开页面
		driver.get(url);
		Thread.sleep(3000);
		JavascriptExecutor js = driver;
		for (int i = 0; i < Integer.valueOf(date); i++) {
			js.executeScript("window.scrollBy(0,1000)");
			//睡眠10s等js加载完成
			Thread.sleep(3000);
		}
		//指定了OutputType.FILE做为参数传递给getScreenshotAs()方法,其含义是将截取的屏幕以文件形式返回。
		File srcFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
		Thread.sleep(3000);
		//利用FileUtils工具类的copyFile()方法保存getScreenshotAs()返回的文件对象
		FileUtils.copyFile(srcFile, new File("C:\\Users\\16630\\Desktop\\html.jpg"));
		//linux中路径
//		FileUtils.copyFile(srcFile, new File("/root/data/html_image/html.jpg"));
		System.out.println("耗时:" + (System.currentTimeMillis() - start) + " 毫秒");
	}


}

参数是网址和js加载时长网页越长需要加载时长越长

效果图

java操作使用phantomjs网页转换图片
百度首页

怎么加载本地静态HTML?

nginx代理静态网页
或者写个服务启动访问服务中静态资源的地址即可