gpt4 book ai didi

java - 如何使用 GhostDriver 和 PhantomJS 减少从 Web 获取 URL 页面的时间

转载 作者:行者123 更新时间:2023-11-29 05:45:57 26 4
gpt4 key购买 nike

我正在用 Maven 做项目。我尝试从 URl 获取页面。到现在为止,我已经成功地从网络上获取页面。但是我有两个问题,

问题

  1. 下面的代码大约需要 14 秒来获取任意两个 URL 页面,我怎样才能减少这个时间,帮助我优化它。
  2. 执行完成后,不会退出代码。为什么 ?我用 driver.close() 结束了代码。那么,为什么,没有成功退出呢。我在开始之前和完成该过程之后添加了快照。请看这些。

帮我解决我的问题。请。

我的代码:-

package XXX.YYY.ZZZ.Template_Matching;

import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import java.io.IOException;
import org.junit.Assert;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriverService;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.w3c.dom.Document;

public class HtmlUnit {

public static void main(String[] args) throws Exception {
String url1 = "http://www.jabong.com/men/shoes/men-loafers/?source=home-leftnav";
String url2 = "http://www.jabong.com/fastrack-9915Pp36J-Black-Pink-Analog-Watch-198499.html";
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, "C://Users//jhamb//Desktop//phantomjs-1.9.0-windows//phantomjs.exe");
WebDriver driver = new PhantomJSDriver(caps);
driver.get(url1);
String hml1 = driver.getPageSource();
driver.get(url2);
String hml2 = driver.getPageSource();
driver.close();
//System.out.println(hml1);
//System.out.println(hml2);
Document doc1 = Jsoup.parse(hml1);
Document doc2 = Jsoup.parse(hml2);
// Some operations using these DOM tree, just like , comparing Templates of two URLS
}
}

开始流程前的快照,

enter image description here

进程完成后的快照,无故等待时,

enter image description here

最佳答案

你需要使用

driver.quit();

代替

driver.close();

关于java - 如何使用 GhostDriver 和 PhantomJS 减少从 Web 获取 URL 页面的时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15852687/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com