java采集网页数据方法【多线程数据采集之一】

第一步抓取数据。


java采集网页数据。获取html文本节点

有几种办法。转载文章请注明来处:http://blog.csdn.net/column/details/threadgrab.html


第一种: 采用HttpURLConnection


[java]  view plain copy
  1. package com.yjf.util;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.IOException;  
  5. import java.io.InputStream;  
  6. import java.io.InputStreamReader;  
  7. import java.net.HttpURLConnection;  
  8. import java.net.URL;  
  9.   
  10. public class HttpWebUtil {  
  11.       
  12.     /** 
  13.      *网页抓取方法 
  14.      * @param urlString      要抓取的url地址 
  15.      * @param charset        网页编码方式 
  16.      * @param timeout        超时时间 
  17.      * @return               抓取的网页内容 
  18.      * @throws IOException   抓取异常 
  19.      */  
  20.     public static String GetWebContent(String urlString, final String charset, int timeout) throws IOException {  
  21.         if (urlString == null || urlString.length() == 0) {  
  22.             return "";  
  23.         }  
  24.         urlString = (urlString.startsWith("http://") || urlString.startsWith("https://")) ? urlString : ("http://" + urlString).intern();  
  25.         URL url = new URL(urlString);  
  26.         HttpURLConnection conn = (HttpURLConnection) url.openConnection();  
  27.         conn.setDoOutput(true);     
  28.         conn.setRequestProperty("Pragma""no-cache");     
  29.         conn.setRequestProperty("Cache-Control""no-cache");     
  30.           
  31.         int temp = Integer.parseInt(Math.round(Math.random()*(UserAgent.length-1))+"");  
  32.         conn.setRequestProperty(  
  33.                 "User-Agent",  
  34.                     UserAgent[temp]);  // 模拟手机系统  
  35.         conn.setRequestProperty("Accept""text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");//只接受text/html类型,当然也可以接受图片,pdf,*/*任意,就是tomcat/conf/web里面定义那些  
  36.         conn.setConnectTimeout(timeout);  
  37.         try {  
  38.             if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {  
  39.                 return "";  
  40.             }  
  41.         } catch (Exception e) {  
  42.             try {  
  43.                 System.out.println(e.getMessage());  
  44.             } catch (Exception e2) {  
  45.                 e2.printStackTrace();  
  46.             }  
  47.             return "";  
  48.         }  
  49.         InputStream input = conn.getInputStream();  
  50.         BufferedReader reader = new BufferedReader(new InputStreamReader(input,  
  51.                 charset));  
  52.         String line = null;  
  53.         StringBuffer sb = new StringBuffer("");  
  54.         while ((line = reader.readLine()) != null) {  
  55.             sb.append(line).append("\r\n");  
  56.         }  
  57.         if (reader != null) {  
  58.             reader.close();  
  59.         }  
  60.         if (conn != null) {  
  61.             conn.disconnect();  
  62.         }  
  63.         return sb.toString();  
  64.     }  
  65.       
  66.     public static String[] UserAgent = {  
  67.         "Mozilla/5.0 (Linux; U; Android 2.2; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.2",  
  68.         "Mozilla/5.0 (iPad; U; CPU OS 3_2_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B500 Safari/531.21.11",  
  69.         "Mozilla/5.0 (SymbianOS/9.4; Series60/5.0 NokiaN97-1/20.0.019; Profile/MIDP-2.1 Configuration/CLDC-1.1) AppleWebKit/525 (KHTML, like Gecko) BrowserNG/7.1.18121",  
  70.         "Nokia5700AP23.01/SymbianOS/9.1 Series60/3.0",  
  71.         "UCWEB7.0.2.37/28/998",  
  72.         "NOKIA5700/UCWEB7.0.2.37/28/977",  
  73.         "Openwave/UCWEB7.0.2.37/28/978",  
  74.         "Mozilla/4.0 (compatible; MSIE 6.0; ) Opera/UCWEB7.0.2.37/28/989"  
  75.     };  
  76.       
  77. }  

第二种:采用jar插件Jsoup.jar 

org.jsoup.Jsoup

七七八八网http://www.qi788.com

[java]  view plain copy
  1. //站点入口列表  
  2.     public static List<String> getSiteUrlList1(List<String> list,String listurl){  
  3.         if(list==null || list.size()<=0){  
  4.             list = new ArrayList<String>();  
  5.         }  
  6.         try {  
  7.             Document docdata = Jsoup.connect(listurl).timeout(10000).get();  
  8.             String hb = ".m_book li a";  
  9.             String page = ".page_list .page_up";  
  10.             Elements ele = docdata.select(hb);  
  11.             for (Element el : ele) {  
  12.                 list.add(el.attr("href"));  
  13.             }  
  14.             if(docdata.select(page)!=null && docdata.select(page).first()!=null){  
  15.                 String url = "http://www.xxxxx.com/site_map/"+docdata.select(page).first().attr("href");  
  16.                 getSiteUrlList1(list, url);  
  17.             }  
  18.         } catch (Exception e) {  
  19.             e.printStackTrace();  
  20.         }  
  21.         return list;  
  22.     }  


第三种:http模拟器

可以请求表单数据和 重定向

http://blog.csdn.net/yjflinchong/article/details/8004706 


以上先描述第一步抓取数据。后期加入多线程网络数据采集完整介绍。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值