google类搜索引擎的抓取解析结果

Crawling & parsing results of querying google-like search engine

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

主要目标是从 Google 学者那里获得一些信息(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");

但我 url 是这样的:

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 库中或在其他库中是更好的主意?请告诉我!我没有任何其他想法,如何解析这样的东西...

您目前使用您的代码得到一个 URL 像这样:

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&hl=plmauthors=Cracow+University+of+Economic&after_author= <b>QPQwAJz___8J</b>

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

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

I have to translate \x signs and add that site to my "toVisit" sites...I don't have any other idea, how to parse something like this...

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

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

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

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