gpt4 book ai didi

java - 如何获取html属性值?

转载 作者:行者123 更新时间:2023-12-02 09:23:33 25 4
gpt4 key购买 nike

我想获取 HTML 代码中“value ”属性的值,其中 class = validated 并将其保存到变量“variationID ”中。我如何使用java编写代码?

以下是 HTML 代码的一部分:

<input id="942981037_3-4" type="text" size="35" value="533" data-valid="-0123456789" class="validated"/>

此处附加文件:https://gofile.io/?c=mNiVcB

这是我创建的初始代码:

String html = prev.getResponseDataAsString(); // get response from your sampler
String variationID;
Integer b;
for (int i = -1; (i = html.indexOf("class=\"validated\"", i + 1)) != -1; i++) {
b = i;
}
b=b-30;
variationID = html.substring(b,b+3);
int res = Integer.parseInt(variationID) + 1;
variationID = res.toString();
log.info(variationID);

最佳答案

为什么不使用正则表达式呢?您可以简单地编写正则表达式来从输入标记中提取属性。请引用下面的代码来提取属性的值。

    String html = prev.getResponseDataAsString(); // get response from your sampler
String variationID;

// Pattern for finding input tag with class validated
String input = "<input id=\"942981037_3-4\" type=\"text\" size=\"35\" value=\"533\" data-valid=\"-0123456789\" class=\"validated\"/>";
// Compiling the pattern
Pattern pattern = Pattern.compile("\\<input.*(class=\".*validated.*\")?.*value=\"([\\w\\d]+)?\".*(class=\".*validated.*\")?.*/\\>");
// Passing the input string to the pattern to start matching
Matcher matcher = pattern.matcher(input);

// Iterate through all the matches in given string
while(matcher.find()) {
// If a match is found value will be found in group 2
variationID = matcher.group(2);
}

关于java - 如何获取html属性值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58497624/

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