首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >HtmlUnit xpath getElement

HtmlUnit xpath getElement
EN

Stack Overflow用户
提问于 2013-02-23 14:11:17
回答 1查看 5K关注 0票数 0

我正在尝试为一个特定的网站创建一个web内容爬虫。

http://v1000.vn/bang-xep-hang?ref=bang-xep-hang-1000-doanh-nghiep-dong-thue-thu-nhap-nhieu-nhat-2012

很快,我的XPath找到了更改页面的链接(使用javascript),导致NullPointExecetion无法工作。我尝试过以不同的方式修改XPath,但是没有什么效果。

另外,在脚本运行之后,我是否需要运行任何方法来获得新页面?

代码语言:javascript
复制
> 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);
        }
        */
    }
}

谢谢你,阮明明

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-02-27 11:13:51

亲爱的,你犯了很小的错误,分号错误

代码语言:javascript
复制
HtmlAnchor link = page.getFirstByXPath("//a[@href='javascript:loadRankingTable(3);']");
link.click();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15041426

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档