2012-10-30 12 views
5

Pracuję nad jedną aplikacją, która pokazuje stopę Gold i tworzy wykres dla tego.
Znajduję jeden website, który zapewnia mi regularnie tę stawkę złota. Moje pytanie brzmi: jak wyodrębnić tę konkretną wartość ze strony html.
Oto link, który muszę wyodrębnić = http://www.todaysgoldrate.co.in/todays-gold-rate-in-pune/, a ta strona html ma następujący tag i treść.Jak uzyskać określoną wartość z html w java?

<p><em>10 gram gold Rate in pune = Rs.31150.00</em></p>  

Oto mój kod, którego używam do wyodrębniania, ale nie znalazłem sposobu na wyodrębnienie określonej treści.

public class URLExtractor { 

private static class HTMLPaserCallBack extends HTMLEditorKit.ParserCallback { 

    private Set<String> urls; 

    public HTMLPaserCallBack() { 
     urls = new LinkedHashSet<String>(); 
    } 

    public Set<String> getUrls() { 
     return urls; 
    } 

    @Override 
    public void handleSimpleTag(Tag t, MutableAttributeSet a, int pos) { 
     handleTag(t, a, pos); 
    } 

    @Override 
    public void handleStartTag(Tag t, MutableAttributeSet a, int pos) { 
     handleTag(t, a, pos); 
    } 

    private void handleTag(Tag t, MutableAttributeSet a, int pos) { 
     if (t == Tag.A) { 
      Object href = a.getAttribute(HTML.Attribute.HREF); 
      if (href != null) { 
       String url = href.toString(); 
       if (!urls.contains(url)) { 
        urls.add(url); 
       } 
      } 
     } 
    } 
} 

public static void main(String[] args) throws IOException { 
    InputStream is = null; 
    try { 
     String u = "http://www.todaysgoldrate.co.in/todays-gold-rate-in-pune/"; 
     //Here i need to extract this content by tag wise or content wise.... 

Dzięki z góry .......

Odpowiedz

3

Można użyć biblioteki jak Jsoup

Można go pobrać stąd ->Download Jsoup

Oto jego odniesienie API ->Jsoup API Reference

Jej naprawdę bardzo łatwe do analizowania zawartości HTML przy użyciu Jsoup.

Poniżej znajduje się przykładowy kod, który może być pomocny dla ciebie ..

public class GetPTags { 

      public static void main(String[] args){ 

      Document doc = Jsoup.parse(readURL("http://www.todaysgoldrate.co.intodays-gold-rate-in-pune/")); 
      Elements p_tags = doc.select("p"); 
      for(Element p : p_tags) 
      { 
       System.out.println("P tag is "+p.text()); 
      } 

      } 

     public static String readURL(String url) { 

     String fileContents = ""; 
     String currentLine = ""; 

     try { 
      BufferedReader reader = new BufferedReader(new InputStreamReader(new URL(url).openStream())); 
      fileContents = reader.readLine(); 
      while (currentLine != null) { 
       currentLine = reader.readLine(); 
       fileContents += "\n" + currentLine; 
      } 
      reader.close(); 
      reader = null; 
     } catch (Exception e) { 
      JOptionPane.showMessageDialog(null, e.getMessage(), "Error Message", JOptionPane.OK_OPTION); 
      e.printStackTrace(); 

     } 

     return fileContents; 
    } 

} 
+0

Dziękuję człowiek .... mam co chcę ... Tak trzymać! –

+0

Nie ma za co :) – Pratik