【问题标题】:Crawling & parsing results of querying google-like search engine查询类google搜索引擎的爬取和解析结果
【发布时间】:2015-05-15 19:48:44
【问题描述】:

我必须用 Java 编写解析器(我的第一个 html 解析器就是这样)。目前我正在使用 jsoup 库,我认为它是解决我的问题的好方法。

主要目标是从 Google Scholar 中获取一些信息(h 指数、出版物数量、从事科学工作的年限)。我知道如何解析 10 个人的 html,如下所示:

http://scholar.google.pl/citations?mauthors=Cracow+University+of+Economics&hl=pl&view_op=search_authors

for( Element element : htmlDoc.select("a[href*=/citations?user") ){
    if( element.hasText() ) {
        String findUrl = element.absUrl("href");
        pagesToVisit.add(findUrl);
    }
}

但我需要找到有关所要求大学的所有科学家的信息。怎么做?我正在考虑从按钮获取 url,这将引导我们获得接下来的 10 个结果,如下所示:

Elements elem = htmlDoc.getElementsByClass("gs_btnPR");
String nextUrl = elem.attr("onclick");

但我得到这样的网址:

citations?view_op\x3dsearch_authors\x26hl\x3dpl\x26oe\x3dLatin2\x26mauthors\x3dAGH+University+of+Science+and+Technology\x26after_author\x3dslQKAC78__8J\x26astart\x3d10

我必须翻译\x 标志并将该网站添加到我的“toVisit”网站?还是在 jsoup 库中或在其他库中可能是一个更好的主意?请告诉我!我没有任何其他想法,如何解析这样的东西......

【问题讨论】:

    标签: java parsing web-crawler jsoup


    【解决方案1】:

    我必须翻译 \x 符号并将该站点添加到我的“toVisit”站点...我没有任何其他想法,如何解析这样的内容...

    \xAAhexadecimal 编码的 ascii。例如\x3d=\x26&。这些值可以使用Integer.parseInt 转换,基数设置为 16。

    char c = (char)Integer.parseInt("\\x3d", 16);
    System.out.println(c); 
    

    如果您需要在没有第三方库的情况下解码这些值,您可以使用正则表达式来完成。例如,使用您的问题中提供的字符串:

    String st = "citations?view_op\\x3dsearch_authors\\x26hl\\x3dpl\\x26oe\\x3dLatin2\\x26mauthors\\x3dAGH+University+of+Science+and+Technology\\x26after_author\\x3dslQKAC78__8J\\x26astart\\x3d10";
    System.out.println("Before Decoding: " + st);
    Pattern p = Pattern.compile("\\\\x([0-9A-Fa-f]{2})");
    Matcher m = p.matcher(st);
    while ( m.find() ){
        String c = Character.toString((char)Integer.parseInt(m.group(1), 16));
        st = st.replaceAll("\\" + m.group(0), c);
        m = p.matcher("After Decoding: " + st);//optional, but added for clarity as st has changed
    }
    System.out.println(st);
    

    【讨论】:

    • 因为 = 和 & 只是十六进制编码中的字符,我使用了简单的解决方案(我不知道它比你快,但确实更简单)。我猜提取以 \x 开头的所有部分会更加优雅和通用。这是我不能使用捷径的充分理由吗? :) nextPageUrl = nextPageUrl.replace("\\x3d", "=").replace("\\x26", "&");
    • Short way 原则上是可行的,只要保证服务器响应(换句话说,代码没有考虑到服务器不会吐出无法识别的编码的可能性)跨度>
    【解决方案2】:

    您目前使用您的代码获得这样的网址:

    citations?view_op\x3dsearch_authors\x26hl\x3dpl\x26oe\x3dLatin2\x26mauthors\x3dAGH+University+of+Science+and+Technology\x26after_author\x3d<b>QPQwAJz___8J</b>\x26astart\x3d10

    您必须提取该粗体部分(使用正则表达式),并使用它来构建获取下一页搜索结果的 URL,如下所示:

    scholar.google.pl/citations?view_op=search_authors&amp;hl=plmauthors=Cracow+University+of+Economic&amp;after_author=<b>QPQwAJz___8J</b>

    然后您可以从此 URL 获取下一页并使用 Jsoup 进行解析,然后重复此操作以获取所有下一个剩余页面。

    稍后会整理一些示例代码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-06
      • 1970-01-01
      • 2011-05-14
      • 1970-01-01
      • 2013-01-30
      • 2021-11-16
      相关资源
      最近更新 更多