当前位置:网站首页>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
边栏推荐
- HMS core news industry solution: let technology add humanistic temperature
- 宏源期货开户安全么?宏源期货公司可以降低手续费?
- Promoting compatibility and adaptation, enabling coordinated development of gbase may adaptation Express
- The MIHA tour club in June is hot! 500+ posts, more than HC, just this summer (with internal promotion method)
- 校企联合在路上!华为云GaussDB又来高校啦
- CVE-2022-0847(提权内核漏洞)
- Rosbag使用命令
- pymssql模块使用指南
- 蓝桥杯2019年国赛最长子序列
- Huawei cloud hcdez special session and Distributed Technology Summit: Huawei cloud distributed cloud native technology and Practice
猜你喜欢

Advanced thinking on application scenarios of standardization, maximum normalization and mean normalization

关于 GIN 的路由树

【LeetCode】9、回文数

Simulation of vector

Devsecops: best practices for ci/cd pipeline security

【newman】postman生成漂亮的测试报告
![[leetcode] 9. Palindromes](/img/58/1817b072949458f9652c144ac4ec0e.png)
[leetcode] 9. Palindromes

Recommend several AI Intelligent Platforms

(pytorch进阶之路二)word embedding 和 position embedding

“软件定义世界,开源共筑未来” 2022开放原子全球开源峰会7月底即将开启
随机推荐
【山大会议】一些基本工具类定义
nvarchar和varchar的区别
Jenkins automatically triggers compilation by checking code submissions
On the routing tree of gin
迷宫问题(BFS记录路径)
基础版现在SQL分析查询不能用了吗?
向量3(静态成员)
New design of databend SQL planner
Advanced thinking on application scenarios of standardization, maximum normalization and mean normalization
Meet webassembly again
UK considers listing arm in London based on national security
OOP 多重收纳(类模板)
"Forget to learn again" shell process control - 38. Introduction to while loop and until loop
Be an we media video blogger, and share the necessary 32 material websites
阿里云中间件的开源往事
关于 GIN 的路由树
Wallys/DR7915-wifi6-MT7915-MT7975-2T2R-support-OpenWRT-802.11AX-supporting-MiniPCIe
Navicat premium connecting to Oracle database (Graphic tutorial)
模板特例化 template<>
2020年蓝桥杯省赛真题-走方格(DP/DFS)