1

我有一个来自脚本的外部 HTML,我需要从其中获取城市列表。

我一直在尝试通过将脚本恢复为 HTML 来获得它:

Document citiesHTML = Jsoup.parse(driver.findElement(By.xpath("/html/body/section/script")).getAttribute("outerHTML"));

该行的返回是:

<html>
 <head>
  <script>
      NCM.Registry.add('PreHomeStatic', 'PreHome_1485892226002', {
        backgroundColor: '#f1f1f1',
        backgroundImage: '',
        subscriberUrl: '/cliente',
        notSubscriberUrl: '/home', 
        defaultCityName: "sao_paulo",
        defaultCityId: '1366122212339',
        cityNotFoundMessage: 'Os serviços NET não estão disponíveis para sua cidade TEST',
        cityPlaceholder: 'Digite Sua Cidade',
        subscriberLabel: 'Já é <b>cliente NET?</b>',
        footerNote: 'Rodap&eacute;',
        cities: [{"id_wcs":"1374010568098","id":"almirante_tamandare","value":"Almirante Tamandaré","tokens":["almirante","tamandare","Almirante","Tamandaré"]},{"id_wcs":"1374019924528","id":"alvorada","value":"Alvorada"...

我需要得到那个“ cities”数组。

4

1 回答 1

0

你有没有尝试过这样的事情?

    Document doc = // JSoup document

    String html = doc.select("script").html();

    String[] lines = html.split("\n");

    for (String line : lines) {
        if (line.trim().startsWith("cities:")){
            System.out.println(line.replaceFirst("cities:", ""));
        }
    }
于 2017-02-03T13:07:09.253 回答