如何使用InputStream和Scanner {Y}从Youtube检索视图计数

时间:2017-04-25 07:07:29

标签: java youtube try-catch inputstream joptionpane

我正在学习java编程的一个小课程,我正在为我们提供URL的工作,我们需要从网页的源代码中检索一些特定的数据并将它们发布在JOptionPane上。我遇到的问题是,扫描后,我不知道如何从源代码中获取数字:

这是我到目前为止所做的事情(它不完整,但我只需要帮助了解如何检索视图数量:

    try {

        URL url1 = new URL("https://www.youtube.com/watch?v=9bZkp7q19f0");
        InputStream viewCount = url1.openStream();
        Scanner read = new Scanner(inTemp);
        String name = read.nextLine();
        String wanted = "watch-view-count ";    
        String noOfViews = null;

        while (read.hasNextLine()) {
            if (name.contains(wanted)) {
                noOfViews = wanted; 
                break;
        }
    } catch (Exception e) {
    }


    String messageInHTML = "<html>Temperature: ";
    messageInHTML += "<li>Relative Humidity: " ;
    messageInHTML += "<li>Hang Seng Index: " ;
    messageInHTML += "<li>Gangnum Style View Count: " ;
    messageInHTML += "</html>";

    int option = JOptionPane.showConfirmDialog(null, messageInHTML, "BillBoard", JOptionPane.OK_CANCEL_OPTION);
    if (option == JOptionPane.OK_OPTION) {
        System.out.println("User clicked OK");
    }
    if (option == JOptionPane.CANCEL_OPTION) {
        System.out.println("User clicked Cancel");
    }

0 个答案:

没有答案