java判断某网页中是否包含某字符串

版权声明:转发请标明出处,谢谢! https://blog.csdn.net/Myuhua/article/details/84974798
public class StringInWeb {
    public static void main(String[] args) throws IOException {
        webLinksContainKeyString("https://www.baidu.com/");
    }
    /**
     * @Description: 在网页中查询一段字符串
     * @Author: yuhua
     * @Date: 2018/12/12
     */
    public static boolean webLinksContainKeyString(String webLinks) throws IOException {
        boolean flag=false;
        InputStream inputStream=null;
        InputStreamReader inputStreamReader=null;
        BufferedReader bufferedReader=null;
        try {
            URL url = new URL(webLinks);
            inputStream =url.openStream();
            inputStreamReader = new InputStreamReader(inputStream);
            bufferedReader = new BufferedReader(inputStreamReader);
            String finalStr="start:";
            String str;
            while ((str = bufferedReader.readLine()) != null) {
                finalStr+=str;
            }
            System.out.println("finalStr:"+finalStr);
            if(finalStr.contains("http://tieba.baidu.com ")){
                flag=true;
                System.out.println("百度页面中包含串:"+flag);
            }else {
                System.out.println("百度页面中不包含串");
                return flag;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            bufferedReader.close();
            inputStreamReader.close();
            inputStream.close();
        }
        return flag;
    }
}

猜你喜欢

转载自blog.csdn.net/Myuhua/article/details/84974798