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