如何使用jsoup访问子类 [英] How to access the subclass using jsoup
问题描述
我要访问此网页: https://www.google.com /trends/explore#q = ice%20cream 并提取中心线图中的数据. html文件是(这里,我只粘贴我使用的部分.):
I want to access this webpage: https://www.google.com/trends/explore#q=ice%20cream and extract the data within in the center line graph. The html file is(Here, I only paste the part that I use.):
<div class="center-col">
<div class="comparison-summary-title-line">...</div>
...
<div id="reportContent" class="report-content">
<!-- This tag handles the report titles component -->
...
<div id="report">
<div id="reportMain">
<div class="timeSection">
<div class = "primaryBand timeBand">...</div>
...
<div aria-lable = "one-chart" style = "position: absolute; ...">
<svg ....>
...
<script type="text/javascript">
var chartData = {...}
我使用的数据存储在脚本部分(最后一行)中.我的想法是先获取类"report-content",然后选择脚本.我的代码如下:
And the data I used is stored in the script part(last line). My idea is to get the class "report-content" first, and then select script. And my code follows as:
String html = "https://www.google.com/trends/explore#q=ice%20cream";
Document doc = Jsoup.connect(html).get();
Elements center = doc.getElementsByClass("center-col");
Element report = doc.getElementsByClass("report-content");
System.out.println(center);
System.out.println(report);
当我打印中心"类时,我可以获得除报告内容"之外的所有子类内容,而当我打印报告内容"时,结果仅是:
When I print "center" class, I can get all the subclasses content except the "report-content", and when I print the "report-content", the result is only like:
<div id="reportContent" Class="report-content"></div>
我也尝试这样做:
Element report = doc.select(div.report-content).first();
,但仍然根本不起作用.我如何在这里获取脚本中的数据?感谢您的帮助!
but still does not work at all. How could I get the data in the script here? I appreciate your help!!!
推荐答案
请尝试使用此网址:
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...
参考文献:
这篇关于如何使用jsoup访问子类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!