JSOUP 解析 iframe Google 地图
JSOUP parse iframe Google Maps
我正在尝试从本网站 http://hdh.ucsd.edu/mobile/dining/locationmap.aspx?l=39 上的 Google 地图解析纬度和经度,但我无法获取任何内容。我怎样才能得到纬度和经度?或者什么查询可以完成这个?
Document document = Jsoup.connect(url).get();
Elements elements = document.select("div.place-name");
String LOCATION = elements.text();
您提供的这个 url 没有 div.place-name
,因为如果您使用 firebug 检查它,它就在 iframe 中。
获取坐标比较难,因为这个URL必须在iframe中使用
iframe 标签有带坐标的 src 标签。如果这些是坐标 q=32.881093211,-117.2406307
您可以使用 JSOUP 获取 iframe 并使用自定义正则表达式提取这两个数字。
您可以从 iframe
标签的 src
属性中读取坐标。 URL 包含 q
参数,该参数将为您提供以逗号分隔的坐标。
package com.github.davidepastore.Whosebug34038430;
import java.io.IOException;
import java.net.URL;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
/**
* Whosebug 34038430 question.
*
*/
public class App {
public static void main(String[] args) throws IOException {
String url = "http://hdh.ucsd.edu/mobile/dining/locationmap.aspx?l=39";
Document document = Jsoup.connect(url).get();
String src = document.select("iframe").first().attr("src");
URL srcUrl = new URL(src);
String result = getParamValue(srcUrl.getQuery());
String[] coordinates = result.split(",");
Double latitude = Double.parseDouble(coordinates[0]);
Double longitude = Double.parseDouble(coordinates[1]);
System.out.println("Latitude: " + latitude);
System.out.println("Longitude: " + longitude);
}
/**
* Get the param value.
* @param query The query string.
* @return Returns the parameter value.
*/
public static String getParamValue(String query) {
String[] params = query.split("&");
for (String param : params) {
String name = param.split("=")[0];
if("q".equals(name)){
return param.split("=")[1];
}
}
return null;
}
}
我正在尝试从本网站 http://hdh.ucsd.edu/mobile/dining/locationmap.aspx?l=39 上的 Google 地图解析纬度和经度,但我无法获取任何内容。我怎样才能得到纬度和经度?或者什么查询可以完成这个?
Document document = Jsoup.connect(url).get();
Elements elements = document.select("div.place-name");
String LOCATION = elements.text();
您提供的这个 url 没有 div.place-name
,因为如果您使用 firebug 检查它,它就在 iframe 中。
获取坐标比较难,因为这个URL必须在iframe中使用
iframe 标签有带坐标的 src 标签。如果这些是坐标 q=32.881093211,-117.2406307
您可以使用 JSOUP 获取 iframe 并使用自定义正则表达式提取这两个数字。
您可以从 iframe
标签的 src
属性中读取坐标。 URL 包含 q
参数,该参数将为您提供以逗号分隔的坐标。
package com.github.davidepastore.Whosebug34038430;
import java.io.IOException;
import java.net.URL;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
/**
* Whosebug 34038430 question.
*
*/
public class App {
public static void main(String[] args) throws IOException {
String url = "http://hdh.ucsd.edu/mobile/dining/locationmap.aspx?l=39";
Document document = Jsoup.connect(url).get();
String src = document.select("iframe").first().attr("src");
URL srcUrl = new URL(src);
String result = getParamValue(srcUrl.getQuery());
String[] coordinates = result.split(",");
Double latitude = Double.parseDouble(coordinates[0]);
Double longitude = Double.parseDouble(coordinates[1]);
System.out.println("Latitude: " + latitude);
System.out.println("Longitude: " + longitude);
}
/**
* Get the param value.
* @param query The query string.
* @return Returns the parameter value.
*/
public static String getParamValue(String query) {
String[] params = query.split("&");
for (String param : params) {
String name = param.split("=")[0];
if("q".equals(name)){
return param.split("=")[1];
}
}
return null;
}
}