当前位置:网站首页>phantomJs使用总结
phantomJs使用总结
2022-06-22 14:45:00 【csdncjh】
下载
maven依赖
<!--phantomjsdriver-->
<dependency>
<groupId>com.codeborne</groupId>
<artifactId>phantomjsdriver</artifactId>
<version>1.2.1</version>
</dependency>使用封装
PhantomJsUtils
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriverService;
import org.openqa.selenium.remote.DesiredCapabilities;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
public class PhantomJsUtils {
private static DesiredCapabilities dcaps = new DesiredCapabilities();
private static final String ua = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36 baidu/3.2.1";
private static boolean isLinux = false;
static {
String os = System.getProperty("os.name");
String path = null;
if (os.toLowerCase().contains("windows")) {
path = "D:\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe";
System.setProperty("webdriver.chrome.driver", path);
} else {
path = "./phantomjs/phantomjs";
System.setProperty("webdriver.chrome.driver", path);
isLinux = true;
}
// 必要参数
System.setProperty("phantomjs.binary.path", path);
// DesiredCapabilities dcaps = new DesiredCapabilities();
//ssl证书支持
dcaps.setCapability("acceptSslCerts", true);
//截屏支持
dcaps.setCapability("takesScreenshot", true);
//css搜索支持
dcaps.setCapability("cssSelectorsEnabled", true);
//js支持
dcaps.setJavascriptEnabled(true);
//驱动支持
dcaps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,
path);
//ua
dcaps.setCapability("phantomjs.page.settings.userAgent", ua);
dcaps.setCapability("phantomjs.page.customHeaders.User-Agent", ua);
//设置连接超时
System.setProperty("sun.net.client.defaultConnectTimeout", "50");
System.setProperty("sun.net.client.defaultReadTimeout", "50");
//优化
List<String> cli = new ArrayList<>();
cli.add("--load-images=" + true);
// cli.add("--output-encoding=" + "utf-8");
cli.add("--disk-cache=yes");
cli.add("--ignore-ssl-errors=true");
cli.add("--debug=false");
dcaps.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, cli);
//代理
/* String ip = "192.168.6.1:1984";
// 启用磁盘缓存(在桌面服务缓存存储位置,默认为false)。也接受:[yes|no]。
// --ignore-ssl-errors=[true|false]忽略SSL错误,例如过期或自签名证书错误(默认为false)。也接受:[yes|no]。
// --proxy=address:port指定要使用的代理服务器(例如--proxy=192.168.1.42:8080)。
String[] values = { "--disk-cache=yes", "--ignore-ssl-errors=true", "--proxy=" + ip };
dcaps.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, values);*/
}
public static PhantomJSDriver initDriver() {
PhantomJSDriver driver = new PhantomJSDriver(dcaps);
//设置隐性等待(作用于全局)
driver.manage().timeouts()
.pageLoadTimeout(20, TimeUnit.SECONDS)
.implicitlyWait(20, TimeUnit.SECONDS);
return driver;
}
/**
* 使用代理
* @param proxy ip:port
* @return
*/
public static PhantomJSDriver initDriver(String proxy) {
DesiredCapabilities dcaps = new DesiredCapabilities();
String os = System.getProperty("os.name");
String path = null;
if (os.toLowerCase().contains("windows")) {
path = "D:\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe";
System.setProperty("webdriver.chrome.driver", path);
} else {
path = "./phantomjs/phantomjs";
System.setProperty("webdriver.chrome.driver", path);
isLinux = true;
}
// 必要参数
System.setProperty("phantomjs.binary.path", path);
// DesiredCapabilities dcaps = new DesiredCapabilities();
//ssl证书支持
dcaps.setCapability("acceptSslCerts", true);
//截屏支持
dcaps.setCapability("takesScreenshot", true);
//css搜索支持
dcaps.setCapability("cssSelectorsEnabled", true);
//js支持
dcaps.setJavascriptEnabled(true);
//驱动支持
dcaps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,
path);
//ua
dcaps.setCapability("phantomjs.page.settings.userAgent", ua);
dcaps.setCapability("phantomjs.page.customHeaders.User-Agent", ua);
//设置连接超时
System.setProperty("sun.net.client.defaultConnectTimeout", "50");
System.setProperty("sun.net.client.defaultReadTimeout", "50");
//优化
List<String> cli = new ArrayList<>();
cli.add("--load-images=" + true);
// cli.add("--output-encoding=" + "utf-8");
cli.add("--disk-cache=yes");
cli.add("--ignore-ssl-errors=true");
cli.add("--debug=false");
dcaps.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, cli);
//代理
// String ip = "192.168.6.1:1984";
// 启用磁盘缓存(在桌面服务缓存存储位置,默认为false)。也接受:[yes|no]。
// --ignore-ssl-errors=[true|false]忽略SSL错误,例如过期或自签名证书错误(默认为false)。也接受:[yes|no]。
// --proxy=address:port指定要使用的代理服务器(例如--proxy=192.168.1.42:8080)。
String[] values = { "--disk-cache=yes", "--ignore-ssl-errors=true", "--proxy=" + proxy };
dcaps.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, values);
PhantomJSDriver driver = new PhantomJSDriver(dcaps);
//设置隐性等待(作用于全局)
driver.manage().timeouts()
.pageLoadTimeout(50, TimeUnit.SECONDS)
.implicitlyWait(50, TimeUnit.SECONDS);
return driver;
}
public static void close(WebDriver driver) {
if (driver != null) {
try {
driver.quit();
} catch (Exception e) {
System.out.println("close error");
}
}
}
/**
* 整页截图
*
* @param driver
* @param destPath
* @throws IOException
*/
public static void screenshot(WebDriver driver, String destPath) throws IOException {
File dfile = new File(destPath);
if(dfile.exists()){
dfile.delete();
}
File outFile = ((PhantomJSDriver) driver).getScreenshotAs(OutputType.FILE);
FileUtils.moveFile(outFile, new File(destPath));
}
public static String getCookieStr(WebDriver driver) {
// driver.navigate().refresh();
String cookieStr = null;
for (Cookie ck : driver.manage().getCookies()) {
cookieStr += (ck.getName() + "=" + ck.getValue() + ";" + ck.getDomain() + ";" + ck.getPath() + ";" + ck.getExpiry() + ";" + ck.isSecure() + ";");
// cookieStr += ck.getName() + "=" + ck.getValue()+";";
}
return cookieStr.substring(0, cookieStr.length() - 1).replaceFirst("null", "");
}
public static void main(String[] args) {
WebDriver driver = initDriver("192.168.6.1:1984");
String url = "https://en.wikipedia.org/wiki/China";
driver.get(url);
System.out.println(driver.getPageSource());
close(driver);
}
}
来源
phantomJs_Selenium_java 最全配置访问_菜鸡java程序员的博客-CSDN博客
Selenium+Phantomjs做Java爬虫_西红柿丶番茄的博客-CSDN博客_java phantomjs selenium
边栏推荐
猜你喜欢

B树和B+树
![[Shangshui Shuo series] day three - VIDEO](/img/42/0911fee2a36f6dda345a571a31acd5.png)
[Shangshui Shuo series] day three - VIDEO

#进程地址空间

Scala language learning-05-a comparison of the efficiency of recursion and tail recursion

Tdengine connector goes online Google Data Studio store

pymssql模块使用指南

vector的模拟实现

Navicat premium connecting to Oracle database (Graphic tutorial)

(pytorch advanced path 2) word embedding and position embedding

Meet webassembly again
随机推荐
“软件定义世界,开源共筑未来” 2022开放原子全球开源峰会7月底即将开启
Exploration and practice of dewu app data simulation platform
CVE-2022-0847(提权内核漏洞)
Be an we media video blogger, and share the necessary 32 material websites
【newman】postman生成漂亮的测试报告
B树和B+树
宏源期货开户安全么?宏源期货公司可以降低手续费?
C language learning -18-makefile file writing examples and how to generate and call dynamic libraries
84. (cesium chapter) movement of cesium model on terrain
Oracle客户端和服务端的区别
Pymssql Module User Guide
#进程地址空间
Trust level of discover
Rosbag使用命令
GBASE现身说 “库” 北京金融科技产业联盟创新应用专委会专题培训
Wechat applet avatar pendant production
排序之归并排序
快速排序quick_sort
Wallys/DR7915-wifi6-MT7915-MT7975-2T2R-support-OpenWRT-802.11AX-supporting-MiniPCIe
乱解码nlp