我正在尝试为特定网站创建网页内容抓取工具
http://v1000.vn/bang-xep-hang?ref=bang-xep-hang-1000-doanh-nghiep-dong-thue-thu-nhap-nhieu-nhat-2012
很快,我的XPath找到更改页面的链接(使用javascript)不起作用导致NullPointExecetion。我试图以各种方式修改XPath,但没有任何效果。
另外,在脚本运行后我是否需要运行任何方法来获取新页面?
> package gimasys.webService;
import java.io.IOException;
import java.net.MalformedURLException;
import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.ThreadedRefreshHandler;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
import com.gargoylesoftware.htmlunit.html.HtmlButton;
import com.gargoylesoftware.htmlunit.html.HtmlLink;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
public class Crawlv1000 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
final WebCrawler wc = new WebCrawler();
final PageCrawler pc = new PageCrawler();
final WebClient webClient = new WebClient(BrowserVersion.CHROME_16);
webClient.setRefreshHandler(new ThreadedRefreshHandler()); // This is to allow handling the page operation using threads else an exception will pop up
try {
HtmlPage page = webClient.getPage("http://v1000.vn/bang-xep-hang?ref=bang-xep-hang-1000-doanh-nghiep-dong-thue-thu-nhap-nhieu-nhat-2012");
HtmlAnchor link = page.getFirstByXPath("//a[@href='javascript:loadRankingTable(3)']");
link.click();
System.out.println(page.getTextContent());
} catch (FailingHttpStatusCodeException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/*
wc.crawl("http://v1000.vn/bang-xep-hang?ref=bang-xep-hang-1000-doanh-nghiep-dong-thue-thu-nhap-nhieu-nhat-2012");
for (String url:wc.urlList)
{
pc.crawl(url);
}
*/
}
}
谢谢, Minh Nguyen
答案 0 :(得分:0)
亲爱的,分号错误,这是一个非常小的错误
HtmlAnchor link = page.getFirstByXPath("//a[@href='javascript:loadRankingTable(3);']");
link.click();