Java獲取網(wǎng)頁(yè)數(shù)據(jù)步驟方法詳解
在很多行業(yè)當(dāng)中,我們需要對(duì)行業(yè)進(jìn)行分析,就需要對(duì)這個(gè)行業(yè)的數(shù)據(jù)進(jìn)行分類,匯總,及時(shí)分析行業(yè)的數(shù)據(jù),對(duì)于公司未來(lái)的發(fā)展,有很好的參照和橫向?qū)Ρ取C媲巴ㄟ^(guò)網(wǎng)絡(luò)進(jìn)行數(shù)據(jù)獲取是一個(gè)很有效而且快捷的方式。首先我們來(lái)簡(jiǎn)單的介紹一下,利用java對(duì)網(wǎng)頁(yè)數(shù)據(jù)進(jìn)行抓取的一些步驟,有不足的地方,還望指正,哈哈。屁話不多說(shuō)了。
其實(shí)一般分為以下步驟:
1:通過(guò)HttpClient請(qǐng)求到達(dá)某網(wǎng)頁(yè)的url訪問(wèn)地址(特別需要注意的是請(qǐng)求方式)
2:獲取網(wǎng)頁(yè)源碼
3:查看源碼是否有我們需要提取的數(shù)據(jù)
4:對(duì)源碼進(jìn)行拆解,一般使用分割,正則或者第三方j(luò)ar包
5:獲取需要的數(shù)據(jù)對(duì)自己創(chuàng)建的對(duì)象賦值
6:數(shù)據(jù)提取保存
下面簡(jiǎn)單的說(shuō)一下在提取數(shù)據(jù)中的部分源碼,以及用途:
/** * 向指定URL發(fā)送GET方法的請(qǐng)求 * * @param url * 發(fā)送請(qǐng)求的URL * @param param * 請(qǐng)求參數(shù),請(qǐng)求參數(shù)應(yīng)該是 name1=value1&name2=value2 的形式。 * @return URL 所代表遠(yuǎn)程資源的響應(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); // 打開(kāi)和URL之間的連接 URLConnection connection = realUrl.openConnection(); // 設(shè)置通用的請(qǐng)求屬性 connection.setRequestProperty('accept', '*/*'); connection.setRequestProperty('connection', 'Keep-Alive'); connection.setRequestProperty('user-agent', 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)'); // 建立實(shí)際的連接 connection.connect(); // 獲取所有響應(yīng)頭字段 Map<String, List<String>> map = connection.getHeaderFields(); // 定義 BufferedReader輸入流來(lái)讀取URL的響應(yīng) in = new BufferedReader(new InputStreamReader( connection.getInputStream())); //這里如果出現(xiàn)亂碼,請(qǐng)使用帶編碼的InputStreamReader構(gòu)造方法,將需要的編碼設(shè)置進(jìn)去 String line; while ((line = in.readLine()) != null) {result += line; } } catch (Exception e) { System.out.println('發(fā)送GET請(qǐng)求出現(xiàn)異常!' + e); e.printStackTrace(); } // 使用finally塊來(lái)關(guān)閉輸入流 finally { try {if (in != null) { in.close();} } catch (Exception e2) {e2.printStackTrace(); } } return result; }
解析存儲(chǔ)數(shù)據(jù)
public Bid getData(String html) throws Exception { //獲取的數(shù)據(jù),存放在到Bid的對(duì)象中,自己可以重新建立一個(gè)對(duì)象存儲(chǔ) 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 = ’詳見(jiàn)內(nèi)容’, itemName = ’訴訟服務(wù)中心設(shè)備采購(gòu)’, item = ’貨物/辦公消耗用品及類似物品/其他辦公消耗用品及類似物品’, itemUnit = ’詳見(jiàn)內(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 = ’’}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. PHP設(shè)計(jì)模式中工廠模式深入詳解2. 得到XML文檔大小的方法3. ASP實(shí)現(xiàn)加法驗(yàn)證碼4. ASP刪除img標(biāo)簽的style屬性只保留src的正則函數(shù)5. ASP基礎(chǔ)知識(shí)Command對(duì)象講解6. jsp+servlet簡(jiǎn)單實(shí)現(xiàn)上傳文件功能(保存目錄改進(jìn))7. 詳細(xì)分析css float 屬性以及position:absolute 的區(qū)別8. PHP循環(huán)與分支知識(shí)點(diǎn)梳理9. PHP session反序列化漏洞超詳細(xì)講解10. html小技巧之td,div標(biāo)簽里內(nèi)容不換行
