JAVA 抓取网页中内容处理
通过JAVA的API可以顺利的抓取网络上的大部分指定的网页内容。最简单的一种抓取方法就是:
??URL urlmy = new URL(myurl);HttpURLConnection con = (HttpURLConnection) urlmy.openConnection();con.setFollowRedirects(true);con.setInstanceFollowRedirects(false);con.connect();BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(),"UTF-8"));String s = "";StringBuffer sb = new StringBuffer("");while ((s = br.readLine()) != null) {sb.append(s+"\r\n");}?
?
?
完整的示例代码??? netpc.java :
????? package cn.com.bps.test;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class netpc {
?public static void main(String[] args) {
??
??//要获取的网页地址
??String myurl = "http://localhost:8080/mydomain/index.jsp";
?
??URL urlmy = null;
??HttpURLConnection con =null;
??try {
???urlmy = new URL(myurl);
???con = (HttpURLConnection) urlmy.openConnection();
??//?con.setFollowRedirects(true);
???con.setInstanceFollowRedirects(false);
???con.connect();
???
???BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(),"UTF-8"));
???String len = "";
???while ((len = br.readLine()) != null) {
????//输出页面上取得的字符串做处理
????System.out.println(len);
???}
???
??} catch (Exception e) {
???// TODO Auto-generated catch block
???e.printStackTrace();
??}
?}
}
?