爬行&解析查询谷歌搜索引擎的结果

时间:2015-05-15 19:48:44

标签: java parsing web-crawler jsoup

我必须用Java编写解析器(通过这种方式我的第一个html解析器)。现在我正在使用jsoup库,我认为这是解决我的问题的非常好的解决方案。

主要目标是从Google学术搜索获取一些信息(h-index,出版物数量,多年的科学载体)。我知道如何用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);
    }
}

但是我需要找到所谓的大学科学家的信息。怎么做?我正在考虑从按钮获取网址,这引导我们接下来的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库中或者在其他库中的mayby中有更好的想法?请告诉我!我没有任何其他想法,如何解析这样的事情......

2 个答案:

答案 0 :(得分:2)

  

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

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

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);

答案 1 :(得分:1)

您目前使用代码获取此类网址:

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

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

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

然后,您可以从此URL获取该下一页并使用Jsoup进行解析,并重复以获取所有下一页。

稍后将汇总一些示例代码。

相关问题