JSOUP 抓取 Java脚本片段 Java

JSOUP Scraping JavaScript piece Java

我正在使用 Jsoup 收集一些数据。 在我的文档中,我有类似的内容:

  <script type="text/javascript">
ta.store('mapsv2.geoName', 'Marseille');
ta.store('mapsv2.map_addressnotfound', 'Address not found');         ta.store('mapsv2.map_addressnotfound3', 'We couldn\'t find that location near {0}.  Please try another search.');       </script> 
  <script type="text/javascript">
window.mapDivId = 'map0Div';
window.map0Div = {
lat: 43.295246,
lng: 5.364188,
zoom: null,
locId: 5039388,
geoId: 187253,

我的代码:

   Document attractionDoc = Jsoup.connect(url).timeout(100000).get();
   System.out.println("attractionDoc "+attractionDoc.toString());

但是我不知道怎么抓到lat:lng:

之后的数字

感谢您的帮助!

JSoup 不解析嵌入的 Javascript,因此没有简单的方法从 window.map0Div 对象中获取对象成员 latlng

但正如@Ceiling Gecko 所指出的,您可以使用其他技术解析脚本标签的内容,例如正则表达式。

假设您将脚本内容作为一个名为 content 的字符串,您可以使用如下内容:

Pattern p = Pattern.compile("window.map0Div\s*=\s*\{.*lat:\s*([0-9.]+),.*lng:\s*([0-9.]+),");
Matcher m = p.matcher(content);
if (m.find()){
    String lat = m.group(1);
    String lng = m.group(2);
    //do whatever you need to do with the info
}

这是一个 fiddle 正则表达式:http://fiddle.re/1p0yd6