這篇文章將為大家詳細(xì)講解有關(guān)利用Java怎么對(duì)網(wǎng)頁(yè)數(shù)據(jù)進(jìn)行獲取,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。
創(chuàng)新互聯(lián)專業(yè)提供成都移動(dòng)機(jī)房托管服務(wù),為用戶提供五星數(shù)據(jù)中心、電信、雙線接入解決方案,用戶可自行在線購(gòu)買(mǎi)成都移動(dòng)機(jī)房托管服務(wù),并享受7*24小時(shí)金牌售后服務(wù)。
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 { h3 = '詳見(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 = '' }
關(guān)于利用Java怎么對(duì)網(wǎng)頁(yè)數(shù)據(jù)進(jìn)行獲取就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。
網(wǎng)站欄目:利用Java怎么對(duì)網(wǎng)頁(yè)數(shù)據(jù)進(jìn)行獲取
網(wǎng)頁(yè)網(wǎng)址:http://www.chinadenli.net/article24/ieooce.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)網(wǎng)站建設(shè)、小程序開(kāi)發(fā)、網(wǎng)站排名、App設(shè)計(jì)、網(wǎng)站維護(hù)、電子商務(wù)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)