简体   繁体   English

在Java中检查HTML的特定行

[英]Check specific line of HTML in Java

I have the following ruby code which checks www.usnwc.org to see if their biking/running trails are open. 我有以下的ruby代码,用于检查www.usnwc.org,以查看其骑行/跑步路线是否打开。 Specifically, this checks an image which changes with the status of the trails: 具体来说,这将检查随路径状态而变化的图像:

require 'open-uri'
content = open('http://usnwc.org/#trail-status').read

if content =~ /trailsclosed\.png/
  puts "Trails closed!"
elsif content =~ /trailsopen\.png/
  puts "Trails open!"
else
  puts "Oops, images not found?" 
end

I would like to perform the same task in Java. 我想在Java中执行相同的任务。 So far I have the following code which gets the page and prints the whole html file to the console. 到目前为止,我有以下代码获取页面并将整个html文件打印到控制台。

public class OpenURL {
    /**
     * @param args the command line arguments
     * @throws java.io.IOException
     */
    public static void main(String[] args) throws IOException {
        URL usnwc = new URL("http://www.usnwc.org/#trail-status");
        try (BufferedReader in = new BufferedReader(new InputStreamReader(
                usnwc.openStream()))) {
            String inputLine;
            while((inputLine = in.readLine()) != null)
                System.out.println(inputLine);
        }
    }
}

How can I check which image is present using Java? 如何使用Java检查存在的图像?

Something like this: 像这样:

if (inputLine.contains("trailsclosed.png")) {
    // trails closed
}

Similar check can be made for trailsopen.png. 可以对Trailsopen.png进行类似的检查。

Once a match is found you can break the while loop. 找到匹配项后,您可以中断while循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM