Sam*_*les 1 android jsoup android-studio
我正在尝试获取 Google 向我们展示的文章数量:
这是对 的 Google 搜索jeb bush barack obama
,它显示了我需要的数量,即10,200,000
文章
如何使用 Jsoup 及其任何组件来获取该数字?
我试过:
Document document = Jsoup.connect(url).get();
Elements description = document.select("div#resultStats");
desc = description.attr("content");
注意:我正在使用 Android Studio,我想将结果保存到矩阵中。
编辑:这是我看到的关于 HTML 源代码的文章数量。
实际上,您可能会得到一些优化的 javascript 代码(适用于现代浏览器),需要运行这些代码才能查看实际结果统计信息。相反,更改您的用户代理字符串(对于最旧的浏览器 UA 字符串)和 url,如下面的代码所示:
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());
Run Code Online (Sandbox Code Playgroud)
About 10,500,000 results
Run Code Online (Sandbox Code Playgroud)
在 Jsoup 1.8.3 上测试
更多 UA 字符串:http : //www.useragentstring.com/
归档时间: |
|
查看次数: |
3324 次 |
最近记录: |