2017-12-12 6 views
1

私は行きたい各ページのクラスに異なる "pid"を持つウェブサイトを持っています。部分クラス名を持つスパンでJsoupクラスを取得する

以下はウェブサイトのHTMLです。 "348" に変更するものである:私はPID番号を残して検索する方法を思っていた

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(); 
} 

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

はコードです。

+0

してください、あなたが参照したり、必要に応じて、完全なHTMLソースの一部にURLを提供することができます。 –

答えて

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"); 
+0

ありがとうございます!私はJSoupを初めて使っていて、どのようにクラスが=うまくいっているのか疑問に思っていました!! – EddiRae

関連する問題