如何使用Jsoup从谷歌获得#resultStats [英] How to get #resultStats from Google using Jsoup
问题描述
我想获得,谷歌向我们显示的条数:
I am trying to get the number of articles that Google shows us:
这是谷歌搜索杰布·布什奥巴马
,它表明我需要的数量,这是 10,200,000
文章
This is a Google search of jeb bush barack obama
, and it shows the number that I need, which is the 10,200,000
articles
我如何使用Jsoup及其任何组件的抢这个数字?
How can I use Jsoup and any of its components to grab that number?
我想:结果
文档文件= Jsoup.connect(URL)获得();
元素描述= document.select(#DIV resultStats);
DESC = description.attr(内容);
请注意:我使用Android的工作室,我想结果保存到矩阵
Note: I am using Android Studio and I want to save the result into a matrix.
编辑:这里是我看到文章的HTML源代码上的数字code。
Here is what I see for the number of articles on the HTML source code.
推荐答案
其实,你可能会得到一些优化的JavaScript code(现代浏览器)需要运行看实际结果统计。相反,改变你的用户代理字符串(一个最古老的浏览器UA字符串),并像在下面的code的网址:
Actually, you may be getting some optimized javascript code (for modern browsers) that need to be run to see actual results stats. Instead, change your user agent string (for a oldest browser UA string) and the url like in the code below:
http://try.jsoup.org/~iYErM3BgfjILVJZshDMkAd-XQCk
String url = "https://www.google.com/search?q=jeb+bush+barack+obama";
Document document = Jsoup //
.connect(url) //
.userAgent("Mozilla/5.0 (Windows; U; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)") //
.get();
Element divResultStats = document.select("div#resultStats").first();
if (divResultStats==null) {
throw new RuntimeException("Unable to find results stats.");
}
System.out.println(divResultStats.text());
输出(在本文写作......)
About 10,500,000 results
测试在Jsoup 1.8.3
更多UA字符串: http://www.useragentstring.com/
这篇关于如何使用Jsoup从谷歌获得#resultStats的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!