从javascript文件中提取信息到远程站点

时间:2016-09-01 10:15:06

标签: javascript java parsing jsoup

我需要提取网站的Html和Javascript中包含的信息。至于html,我通过使用名为jsoup的java库成功实现了这一点,但现在我想从同一站点推断js文件中变量的内容。

我该怎么办?提前致谢

1 个答案:

答案 0 :(得分:2)

  

我想从同一站点推断js文件中变量的内容开始

试试这个:

// ** Exception handling removed ** //

Document doc = Jsoup.connect(websiteUrl).get();

String jsFilesCssQuery = "script[src]";
for(Element script : doc.select(jsFilesCssQuery) {
    // You may add further checks on the script element found here...
    // ...

    // Download JS code
    Connection.Response response = Jsoup //
      .connect(script.absUrl("src")) //
      .userAgent("Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36") //
      .ignoreContentType(true) // To force Jsoup download the JS code
      .referrer(doc.location()) //
      .execute(); //

   String jsCode = new String( //
          response.bodyAsBytes(), //
          Charset.forName(response.charset()) //
   );

   // Do extraction on jsCode here...
   // ...
}