如何从脚本的外部 html 中获取价值
Posted
技术标签:
【中文标题】如何从脚本的外部 html 中获取价值【英文标题】:How to get value from script's outer html 【发布时间】:2017-06-18 04:48:26 【问题描述】:我有一个来自脚本的外部 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é',
cities: ["id_wcs":"1374010568098","id":"almirante_tamandare","value":"Almirante Tamandaré","tokens":["almirante","tamandare","Almirante","Tamandaré"],"id_wcs":"1374019924528","id":"alvorada","value":"Alvorada"...
我需要得到那个“cities
”数组。
【问题讨论】:
【参考方案1】:你尝试过类似的方法吗?
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:", ""));
【讨论】:
以上是关于如何从脚本的外部 html 中获取价值的主要内容,如果未能解决你的问题,请参考以下文章