gpt4 book ai didi

java - Jsoup 在 span 中获取带有部分类名的类

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

我有一个网站,在我需要访问的每个页面的类中都有不同的“pid”。

以下是网站 HTML。 “348”的变化是:

 <span class="arial_20 redFont   pid-348-pc" dir="ltr">-5.60</span>

下面是代码:

Document doc1;
try
{
doc1 = Jsoup.connect(sChartLink).get();
String Title = doc1.title();
System.out.println("Title = " + Title + "\n");

String sCurrentPrice = doc1.select("span#last_last").text(); //Get Current Price
System.out.println("Current Price = " + sCurrentPrice + "\n");

String sPriceChange = doc1.select("span[class=arial_20 redFont pid-348-pc]").text(); //Get Price Change
System.out.println("Price Change = " + sPriceChange + "\n");
}
catch (IOException e) {
e.printStackTrace();
}

我想知道如何搜索而不包含 pid 号。

最佳答案

您可以使用 CSS 选择器指定部分类名称。

例如:

String html = "<html>" +
"<span class=\"arial_20 redFont pid-348-pc\" dir=\"ltr\">-5.60</span>" +
"<span class=\"arial_20 redFont \" dir=\"ltr\">55.80</span>" +
"</html>";

Document doc = Jsoup.parse(html);

// this will print out -5.60 since the only span with a class matching 'arial_20 redFont pid-*'
// is the one with the value: -5.60
// the other span does not match that CSS selector
String sPriceChange = doc.select("span[class*=\"arial_20 redFont pid-\"]").text();
System.out.println("Price Change = " + sPriceChange + "\n");

关于java - Jsoup 在 span 中获取带有部分类名的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47764664/

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