如何从java中的html获取特定值?

前端之家收集整理的这篇文章主要介绍了如何从java中的html获取特定值?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我正在开发一个应用程序,显示黄金率并为此创建图表.
我找到一个website定期提供这个黄金价格.我的问题是如何从html页面提取这个特定的价值.
这是我需要提取链接= http://www.todaysgoldrate.co.in/todays-gold-rate-in-pune/,这个html页面有以下标签内容.

这是我用于提取代码,但我没有找到提取特定内容方法.

  1. public class URLExtractor {
  2. private static class HTMLPaserCallBack extends HTMLEditorKit.ParserCallback {
  3. private Set

提前致谢…….

最佳答案
您可以使用像Jsoup这样的库

你可以从这里得到它 – > Download Jsoup

这是它的API参考 – > Jsoup API Reference

使用Jsoup解析HTML内容非常容易.

以下示例代码可能对您有所帮助..

  1. public class GetPTags {
  2. public static void main(String[] args){
  3. Document doc = Jsoup.parse(readURL("http://www.todaysgoldrate.co.intodays-gold-rate-in-pune/"));
  4. Elements p_tags = doc.select("p");
  5. for(Element p : p_tags)
  6. {
  7. System.out.println("P tag is "+p.text());
  8. }
  9. }
  10. public static String readURL(String url) {
  11. String fileContents = "";
  12. String currentLine = "";
  13. try {
  14. BufferedReader reader = new BufferedReader(new InputStreamReader(new URL(url).openStream()));
  15. fileContents = reader.readLine();
  16. while (currentLine != null) {
  17. currentLine = reader.readLine();
  18. fileContents += "\n" + currentLine;
  19. }
  20. reader.close();
  21. reader = null;
  22. } catch (Exception e) {
  23. JOptionPane.showMessageDialog(null,e.getMessage(),"Error Message",JOptionPane.OK_OPTION);
  24. e.printStackTrace();
  25. }
  26. return fileContents;
  27. }
  28. }

猜你在找的HTML相关文章