1. 程式人生 > >Java PhantomJs下載網頁

Java PhantomJs下載網頁

1 下載PhantomJS

國外的網站下載比較慢,推薦使用淘寶映象下載,地址:

2  下載PhantomJSDriver

這裡使用maven來直接依賴:


            <dependency>
                <groupId>com.codeborne</groupId>
                <artifactId>phantomjsdriver</artifactId>
                <version>1.3.0</version>
            </dependency>

3 使用Junit來測試下載結果:

import java.util.concurrent.TimeUnit;

import org.junit.*;

import static org.junit.Assert.*;

import org.openqa.selenium.*;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.remote.DesiredCapabilities;

public class PhantomJSTest {
  private WebDriver driver;
  private String baseUrl;
  private StringBuffer verificationErrors = new StringBuffer();
  protected static DesiredCapabilities dCaps;

  @Before
  public void setUp() throws Exception {
	  // 指定PhantomJS 可執行程式的位置
		if("Linux".equals(System.getProperty("os.name"))){
			System.setProperty("phantomjs.binary.path", "phantomjs/phantomjs");
		}else{
			System.setProperty("phantomjs.binary.path", "phantomjs/phantomjs.exe");
		}
	
    dCaps = new DesiredCapabilities();
    dCaps.setJavascriptEnabled(true);
    dCaps.setCapability("takesScreenshot", false);

    //<code>dCaps.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, new String[] {"--web-security=no", "--ignore-ssl-errors=yes"});</code>
    driver = new PhantomJSDriver(dCaps);
    baseUrl = "https://jlkone.1688.com/page/creditdetail.htm";
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  }

  @Test
  public void getLinksOfAssertSelenium() throws Exception {
    driver.get(baseUrl + "/");
    
    //Printing the size, will print the no of links present in the page.
    System.out.println("page source: "+driver.getPageSource());
    
  }

  @After
  public void tearDown() throws Exception {
    driver.quit();
    String verificationErrorString = verificationErrors.toString();
    if (!"".equals(verificationErrorString)) {
      fail(verificationErrorString);
    }
  }

}

OK,點選執行Junit,網頁就下載下來了。

參考文獻: