读取源代码
public String getDocumentAt(String urlString) {
StringBuffer document = new StringBuffer();
try {
URL url = new URL(urlString);
URLConnection conn = url.openConnection();
BufferedReader reader =
new BufferedReader(
new InputStreamReader(conn.getInputStream())); String line = null;
while ((line = reader.readLine()) != null) {
document.append(line);
} reader.close();
} catch (MalformedURLException e) {
System.out.println("Unable to connect to URL: " + urlString);
} catch (IOException e) {
System.out.println(
"IOException when connecting to URL: " + urlString);
}
return document.toString().toLowerCase();
}
统计出源代码后,对他进行分析,可以去除html标签,如删除<style", "/style>之间的内容,很好实现的,也可以用正则表达式。
public String getDocumentAt(String urlString) {
StringBuffer document = new StringBuffer();
try {
URL url = new URL(urlString);
URLConnection conn = url.openConnection();
BufferedReader reader =
new BufferedReader(
new InputStreamReader(conn.getInputStream())); String line = null;
while ((line = reader.readLine()) != null) {
document.append(line);
} reader.close();
} catch (MalformedURLException e) {
System.out.println("Unable to connect to URL: " + urlString);
} catch (IOException e) {
System.out.println(
"IOException when connecting to URL: " + urlString);
}
return document.toString().toLowerCase();
}
统计出源代码后,对他进行分析,可以去除html标签,如删除<style", "/style>之间的内容,很好实现的,也可以用正则表达式。
解决方案 »
- Exception in thread "main" java.lang.NullPointerException大神们这是什么问题
- 请教大家,这段js代码是什么意思?
- (急)请哪位仁兄帮忙解决一道多线程与输入输出的综合问题?
- 如何把字符串转化为double 或是float 形
- 请教:关于在JAVA应用程序中调用服务器的JSP页面,并返回执行结果的问题。
- 跪地求教!请帮帮忙,在线等!
- 请问,怎么才能直接调用outlook中的“新邮件”窗口,来发送邮件。如何直接将对方邮件地址传入。(比如网页上提供的该公司email发送)。
- 程序员未来的发展方向!!!
- 在jb7中打开一个.java文件,请问如何编译成一个project?
- JTree控件方面问题
- 急救
- 数据库连接出错怎么回事啊?
import java.net.*;class Sina {
public static void main(String[] args)
throws Exception{
URL url=new URL("http://localhost/index.html");
InputStreamReader isr=new InputStreamReader(url.openStream());
BufferedReader br=new BufferedReader(isr);
FileWriter fr=new FileWriter("c:\\csdn.htm");
PrintWriter bw=new PrintWriter(fr,true);
String s=null;
StringBuffer sb=new StringBuffer();
while((s=br.readLine())!=null){
bw.println(s);
sb.append(s);
}
System.out.println ("长度为"+sb.length());
}
}
用URLConnect类就能很方便解决。