且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用jsoup访问子类

更新时间:2023-12-03 21:52:40

请尝试使用此网址:

https://www.google.com/trends/trendsReport?hl=en&q=${keywords}&tz=${timezone}&content=1

其中

  • ${keywords}是一个用空格分隔的编码关键字列表
  • ${timezone}是Etc/GMT *格式的编码时区
  • ${keywords} is an encoded space separated keywords list
  • ${timezone} is an encoded timezone in the Etc/GMT* form

演示

String myKeywords = "ice cream";
String myTimezone = "Etc/GMT+2";

String url = "https://www.google.com/trends/trendsReport?hl=en&q=" + URLEncoder.encode(keywords, "UTF-8") +"&tz="+URLEncoder.encode(myTimezone, "UTF-8")+"&content=1";

Document doc = Jsoup.connect(url).timeout(10000).get();
Element scriptElement = doc.select("div#TIMESERIES_GRAPH_0-time-chart + script").first();

if (scriptElement==null) {
   throw new RuntimeException("Unable to locate trends data.");
}

String jsCode = scriptElement.html(); 
// parse jsCode to extract charData...

参考文献: