【问题标题】:How t get specific value from html in java?java - 如何从java中的html获取特定值?
【发布时间】:2023-03-08 09:25:02
【问题描述】:

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

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

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

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....  

在此先感谢.......

【问题讨论】:

    标签: java html extract


    【解决方案1】:

    你可以使用像Jsoup这样的库

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

    这里是它的 API 参考 --> Jsoup API Reference

    使用 Jsoup 解析 HTML 内容真的非常容易。

    下面是一个可能对你有帮助的示例代码..

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

    【讨论】:

      【解决方案2】:

      http://java-source.net/open-source/crawlers

      你可以使用任何一个api,但不要用纯JDK解析HTML,因为它太痛苦了。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-06-03
        • 2022-01-21
        • 2014-05-10
        • 2015-06-01
        • 1970-01-01
        • 2019-06-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多