如何使用 Jsoup 从 Google 获取#resultStats [英] How to get #resultStats from Google using Jsoup
问题描述
我正在尝试获取 Google 向我们展示的文章数量:
I am trying to get the number of articles that Google shows us:
This 是对 jeb Bush barack obama 的 Google 搜索
,它显示了我需要的数量,也就是 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?
我试过了:Document document = Jsoup.connect(url).get();Elements description = document.select("div#resultStats");desc = description.attr("content");
注意:我使用的是 Android Studio,我想将结果保存到矩阵中.
Note: I am using Android Studio and I want to save the result into a matrix.
这里是我看到的关于 HTML 源代码的文章数量代码.
Here is what I see for the number of articles on the HTML source code.
推荐答案
实际上,您可能会得到一些优化的 javascript 代码(适用于现代浏览器),需要运行这些代码才能查看实际结果统计信息.相反,更改您的用户代理字符串(对于最旧的浏览器 UA 字符串)和 url,如下面的代码所示:
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 从 Google 获取#resultStats的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!