小弟希望学习写爬虫,哪位大神能帮忙写一个获取html的例子程序吗 小弟希望学习写爬虫,哪位大神能帮忙写一个获取html的例子程序吗?好像给制定的URL,“www.baidu.com”,程序能把这个网页保存到一个html文件 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 正好手头上有一个,希望对你有帮助 private String getListHtml(String listUrl) throws IOException { String sHtml = ""; URLConnection uc = null; BufferedReader br = null; try { java.net.URL url = new URL(listUrl); uc = url.openConnection(); //设置模拟浏览器浏览参数,防止部分网站阻止 uc.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)"); uc.connect(); boolean bgCatch = false; boolean edCatch = false; String line = ""; br = new BufferedReader(new InputStreamReader(new DataInputStream( uc.getInputStream()), "UTF8"));//编码视具体情况而定 while ((line = br.readLine()) != null) { System.out.println(line);//自己处理获得的每行HTML内容 sHtml += line + "\r\n"; } } catch (MalformedURLException e) { e.printStackTrace(); throw new IOException("该地址格式不正确!"); } catch (ConnectException e) { e.printStackTrace(); throw new IOException("该地址不可到达!"); } finally { try { if (br != null) br.close(); } catch (Exception e) { e.printStackTrace(); } } return sHtml; } 楼主也可以考虑下用httpClient.jar 急求答案 组件显示 如何接受从命令行输入的数? java中如何实现每星期一运行一下预定的程序 CallableStatement如何获取SQL2000中存储过程的返回列表 读取网络文件问题 求jdk源代码下载 请问想设计一个表格,其中有一个字段是包括有下拉式列表的,用LookupBox实现,怎样来实现?谢谢 throws怎么使用?谢谢 Java 急,跪求一个java正则表达式 将RESULT传给10个Thread运行问题
String sHtml = "";
URLConnection uc = null;
BufferedReader br = null;
try {
java.net.URL url = new URL(listUrl);
uc = url.openConnection(); //设置模拟浏览器浏览参数,防止部分网站阻止
uc.setRequestProperty("User-Agent",
"Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
uc.connect(); boolean bgCatch = false;
boolean edCatch = false;
String line = ""; br = new BufferedReader(new InputStreamReader(new DataInputStream(
uc.getInputStream()), "UTF8"));//编码视具体情况而定 while ((line = br.readLine()) != null) {
System.out.println(line);//自己处理获得的每行HTML内容
sHtml += line + "\r\n";
}
} catch (MalformedURLException e) {
e.printStackTrace();
throw new IOException("该地址格式不正确!");
} catch (ConnectException e) {
e.printStackTrace();
throw new IOException("该地址不可到达!");
} finally {
try {
if (br != null)
br.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return sHtml;
}