当前位置:网站首页>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
边栏推荐
- 向量2(友元及拷贝构造)
- 又可以这样搞nlp(分类)
- Rosbag use command
- Quickly play ci/cd graphical choreography
- Ultimate efficiency is the foundation for the cloud native database tdsql-c to settle down
- 米哈游六月社招火热开启!500+岗位,超多HC,就在这个夏天(附内推方式)
- 打新债安全性有多高
- UK considers listing arm in London based on national security
- Wallys/DR7915-wifi6-MT7915-MT7975-2T2R-support-OpenWRT-802.11AX-supporting-MiniPCIe
- Scala语言学习-04-函数作为参数传入函数-函数作为返回值
猜你喜欢
米哈游六月社招火热开启!500+岗位,超多HC,就在这个夏天(附内推方式)

Wallys/DR7915-wifi6-MT7915-MT7975-2T2R-support-OpenWRT-802.11AX-supporting-MiniPCIe

Cross border integration, creativity and innovation to help improve the influence of cultural tourism night tour

uni开发微信小程序自定义相机自动检测(人像+身份证)

UK considers listing arm in London based on national security

Bridging the gap between open source databases and database services

各位学弟学妹,别再看教材了,时间复杂度看这篇就好了

Jenkins automatically triggers compilation by checking code submissions

Self inspection is recommended! The transaction caused by MySQL driver bug is not rolled back. Maybe you are facing this risk!

CVE-2022-0847(提权内核漏洞)
随机推荐
OOP 多重收纳(类模板)
Be an we media video blogger, and share the necessary 32 material websites
Recommend several AI Intelligent Platforms
Development status of full color LED display
On the routing tree of gin
Pymssql Module User Guide
UK considers listing arm in London based on national security
js中const定义变量及for-of和for-in
使用 zipfile、openpyxl、flask 批量导出excel zip
新版负载均衡WebClient CRUD
对领域驱动设计DDD理解
洛谷P2466 [SDOI2008] Sue 的小球 题解
Rosbag使用命令
Scala语言学习-05-递归和尾递归效率对比
向量6(继承)
Jenkins 通过检查代码提交自动触发编译
The difference between nvarchar and varchar
百行代码实现基于Redis的可靠延迟队列
问一下想获取到sqlserver的start_lsn有好的办法吗?
HMS core news industry solution: let technology add humanistic temperature