我正在开发一个应用程序,显示黄金率并为此创建图表.
我找到一个website定期提供这个黄金价格.我的问题是如何从html页面中提取这个特定的价值.
这是我需要提取的链接= http://www.todaysgoldrate.co.in/todays-gold-rate-in-pune/,这个html页面有以下标签和内容.
@H_301_8@
@H_301_8@public class URLExtractor { private static class HTMLPaserCallBack extends HTMLEditorKit.ParserCallback { private Set
提前致谢…….
最佳答案
您可以使用像Jsoup这样的库
你可以从这里得到它 – > Download Jsoup
这是它的API参考 – > Jsoup API Reference
使用Jsoup解析HTML内容非常容易.
以下示例代码可能对您有所帮助..
@H_301_8@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; } }