Java獲取網(wǎng)頁數(shù)據(jù)步驟方法詳解
在很多行業(yè)當(dāng)中,我們需要對行業(yè)進行分析,就需要對這個行業(yè)的數(shù)據(jù)進行分類,匯總,及時分析行業(yè)的數(shù)據(jù),對于公司未來的發(fā)展,有很好的參照和橫向?qū)Ρ取C媲巴ㄟ^網(wǎng)絡(luò)進行數(shù)據(jù)獲取是一個很有效而且快捷的方式。首先我們來簡單的介紹一下,利用java對網(wǎng)頁數(shù)據(jù)進行抓取的一些步驟,有不足的地方,還望指正,哈哈。屁話不多說了。
其實一般分為以下步驟:
1:通過HttpClient請求到達某網(wǎng)頁的url訪問地址(特別需要注意的是請求方式)
2:獲取網(wǎng)頁源碼
3:查看源碼是否有我們需要提取的數(shù)據(jù)
4:對源碼進行拆解,一般使用分割,正則或者第三方j(luò)ar包
5:獲取需要的數(shù)據(jù)對自己創(chuàng)建的對象賦值
6:數(shù)據(jù)提取保存
下面簡單的說一下在提取數(shù)據(jù)中的部分源碼,以及用途:
/** * 向指定URL發(fā)送GET方法的請求 * * @param url * 發(fā)送請求的URL * @param param * 請求參數(shù),請求參數(shù)應(yīng)該是 name1=value1&name2=value2 的形式。 * @return URL 所代表遠程資源的響應(yīng)結(jié)果 */ public static String sendGet(String url, String param) { String result = ''; BufferedReader in = null; try { String urlNameString = url; URL realUrl = new URL(urlNameString); // 打開和URL之間的連接 URLConnection connection = realUrl.openConnection(); // 設(shè)置通用的請求屬性 connection.setRequestProperty('accept', '*/*'); connection.setRequestProperty('connection', 'Keep-Alive'); connection.setRequestProperty('user-agent', 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)'); // 建立實際的連接 connection.connect(); // 獲取所有響應(yīng)頭字段 Map<String, List<String>> map = connection.getHeaderFields(); // 定義 BufferedReader輸入流來讀取URL的響應(yīng) in = new BufferedReader(new InputStreamReader( connection.getInputStream())); //這里如果出現(xiàn)亂碼,請使用帶編碼的InputStreamReader構(gòu)造方法,將需要的編碼設(shè)置進去 String line; while ((line = in.readLine()) != null) {result += line; } } catch (Exception e) { System.out.println('發(fā)送GET請求出現(xiàn)異常!' + e); e.printStackTrace(); } // 使用finally塊來關(guān)閉輸入流 finally { try {if (in != null) { in.close();} } catch (Exception e2) {e2.printStackTrace(); } } return result; }
解析存儲數(shù)據(jù)
public Bid getData(String html) throws Exception { //獲取的數(shù)據(jù),存放在到Bid的對象中,自己可以重新建立一個對象存儲 Bid bid = new Bid(); //采用Jsoup解析 Document doc = Jsoup.parse(html); // System.out.println('doc內(nèi)容' + doc.text()); //獲取html標(biāo)簽中的內(nèi)容tr Elements elements = doc.select('tr'); System.out.println(elements.size() + '****條'); //循環(huán)遍歷數(shù)據(jù) for (Element element : elements) { if (element.select('td').first() == null){continue; } Elements tdes = element.select('td'); for(int i = 0; i < tdes.size(); i++){this.relation(tdes,tdes.get(i).text(),bid,i+1); } } return bid; }
得到的數(shù)據(jù)
Bid { h2 = ’詳見內(nèi)容’, itemName = ’訴訟服務(wù)中心設(shè)備采購’, item = ’貨物/辦公消耗用品及類似物品/其他辦公消耗用品及類似物品’, itemUnit = ’詳見內(nèi)容’, areaName = ’港北區(qū)’, noticeTime = ’2018年10月22日 18:41’, itemNoticeTime = ’null’, itemTime = ’null’, kaibiaoTime = ’2018年10月26日 09:00’, winTime = ’null’, kaibiaoDiDian = ’null’, yusuanMoney = ’¥67.00元(人民幣)’, allMoney = ’null’, money = ’null’, text = ’’}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. jsp網(wǎng)頁實現(xiàn)貪吃蛇小游戲2. jsp+servlet簡單實現(xiàn)上傳文件功能(保存目錄改進)3. JavaScript實現(xiàn)組件化和模塊化方法詳解4. ASP.NET MVC遍歷驗證ModelState的錯誤信息5. HTML5 Canvas繪制圖形從入門到精通6. .Net Core和RabbitMQ限制循環(huán)消費的方法7. 淺談SpringMVC jsp前臺獲取參數(shù)的方式 EL表達式8. SpringMVC+Jquery實現(xiàn)Ajax功能9. ASP中if語句、select 、while循環(huán)的使用方法10. asp(vbs)Rs.Open和Conn.Execute的詳解和區(qū)別及&H0001的說明
