gpt4 book ai didi

java - 如何使用 Jsoup 获取脚本条目内的变量

转载 作者:行者123 更新时间:2023-11-30 06:22:52 24 4
gpt4 key购买 nike

我正在使用Jsoup来爬取数据。

部分页面源代码如下所示:

<script> var uitkformatter = { dependency: ['uitk_localized_dateApi', 'uitk_localized_priceApi', 'uitk_localized_config'] }; </script><script async defer src="//www.expedia.com/i18n/28/en_US/JPY/currencyFormats.js?module=exp_currencyformats_JPY"></script><script> define('exp_currencyformats', [ 'exp_currencyformats_JPY' ], function() { return window.uitkformatter; }); </script><script async defer src="//b.travel-assets.com/uitoolkit/2-164/3542359672ff5cd9d827c16bd754bf539fd383b1/core/js/uitk-localize-bundle-min.js"></script>
<script language="javascript" type="text/javascript">
OlAltLang = 'en-us.';
</script>
<script type="text/javascript">
'use strict';
require('infositeApplication', function(infositeApplication) {
infositeApplication.start();
});
define('infosite/env', function() {
return {
isJP: true,
isVN: false,
isVSC:false,
isTD:false
};
});
define('infositeData', [], function() {
var infosite = {};
infosite.hotelId = '5522663';
infosite.guid = '59ad4387-979f-477a-901a-6070f3879ce6';
infosite.token = '6a06f2f73106c754340f7a459f5d75d588637caa'; <--This I need to fetch

如果我想获取 infosite.token 那么我该怎么办?

示例代码:

//Fetch HTML code
Document document = Jsoup.connect(URL).get();
//Parse the HTML to extract links to other URLs
Elements linksOnPage = document.select("QUERY TO FETCH infosite.token");

我不知道我应该在“QUERY TO FETCH infosite.token”中写什么

我试过了

元素 linksOnPage = document.select("script:contains(infosite.token)").first();但不工作。

最佳答案

您可以找到所有 script 元素,如下所示:

Elements scriptElements = doc.getElementsByTag("script");

然后,您可以遍历脚本元素并使用正则表达式查找变量分配(例如 infosite.token = '6a06f2f73106c754340f7a459f5d75d588637caa';),然后获取相关变量的右侧作业。

例如,以下代码将打印出'6a06f2f73106c754340f7a459f5d75d588637caa':

String html =
"<html><script> var uitkformatter = { dependency: ['uitk_localized_dateApi', 'uitk_localized_priceApi', 'uitk_localized_config'] }; </script><script async defer src=\"//www.expedia.com/i18n/28/en_US/JPY/currencyFormats.js?module=exp_currencyformats_JPY\"></script><script> define('exp_currencyformats', [ 'exp_currencyformats_JPY' ], function() { return window.uitkformatter; }); </script><script async defer src=\"//b.travel-assets.com/uitoolkit/2-164/3542359672ff5cd9d827c16bd754bf539fd383b1/core/js/uitk-localize-bundle-min.js\"></script>\n" +
"<script language=\"javascript\" type=\"text/javascript\">\n" +
"OlAltLang = 'en-us.';\n" +
"</script>\n" +
"<script type=\"text/javascript\">\n" +
"'use strict';\n" +
"require('infositeApplication', function(infositeApplication) {\n" +
"infositeApplication.start();\n" +
"});\n" +
"define('infosite/env', function() {\n" +
"return {\n" +
"isJP: true,\n" +
"isVN: false,\n" +
"isVSC:false,\n" +
"isTD:false\n" +
"};\n" +
"});\n" +
"define('infositeData', [], function() {\n" +
"var infosite = {};\n" +
"infosite.hotelId = '5522663';\n" +
"infosite.guid = '59ad4387-979f-477a-901a-6070f3879ce6';\n" +
"infosite.token = '6a06f2f73106c754340f7a459f5d75d588637caa'; </script></html>";

Document doc = Jsoup.parse(html);

Elements scriptElements = doc.getElementsByTag("script");

// the script elements have no identifying charateristic so we must loop
// until we find the one which contains the "infosite.token" variable
for (Element element : scriptElements) {
if (element.data().contains("infosite.token")) {
// find the line which contains 'infosite.token = <...>;'
Pattern pattern = Pattern.compile(".*infosite\\.token = ([^;]*);");
Matcher matcher = pattern.matcher(element.data());
// we only expect a single match here so there's no need to loop through the matcher's groups
if (matcher.find()) {
System.out.println(matcher.group(1));
} else {
System.err.println("No match found!");
}
break;
}
}

关于java - 如何使用 Jsoup 获取脚本条目内的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47766187/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com