我有一个网站,每个页面都有一个不同的“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号码。
答案 0 :(得分:1)
您可以使用指定部分类名称的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");