国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術(shù)文章
文章詳情頁

Java獲取網(wǎng)頁數(shù)據(jù)步驟方法詳解

瀏覽:46日期:2022-09-03 17:03:35

在很多行業(yè)當(dāng)中,我們需要對行業(yè)進(jìn)行分析,就需要對這個(gè)行業(yè)的數(shù)據(jù)進(jìn)行分類,匯總,及時(shí)分析行業(yè)的數(shù)據(jù),對于公司未來的發(fā)展,有很好的參照和橫向?qū)Ρ取C媲巴ㄟ^網(wǎng)絡(luò)進(jìn)行數(shù)據(jù)獲取是一個(gè)很有效而且快捷的方式。首先我們來簡單的介紹一下,利用java對網(wǎng)頁數(shù)據(jù)進(jìn)行抓取的一些步驟,有不足的地方,還望指正,哈哈。屁話不多說了。

其實(shí)一般分為以下步驟:

1:通過HttpClient請求到達(dá)某網(wǎng)頁的url訪問地址(特別需要注意的是請求方式)

2:獲取網(wǎng)頁源碼

3:查看源碼是否有我們需要提取的數(shù)據(jù)

4:對源碼進(jìn)行拆解,一般使用分割,正則或者第三方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 所代表遠(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); // 打開和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)'); // 建立實(shí)際的連接 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è)置進(jìn)去 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的對象中,自己可以重新建立一個(gè)對象存儲 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)。

標(biāo)簽: Java
相關(guān)文章:
主站蜘蛛池模板: 永顺县| 江门市| 丹阳市| 赤峰市| 正镶白旗| 什邡市| 金阳县| 驻马店市| 湟源县| 扬州市| 修水县| 河南省| 通许县| 清苑县| 洛宁县| 翁源县| 屏南县| 大化| 措勤县| 马公市| 新邵县| 宜兰市| 康平县| 白城市| 白银市| 通化市| 古蔺县| 寿光市| 仙桃市| 靖边县| 安塞县| 酒泉市| 通许县| 尉犁县| 班戈县| 章丘市| 察隅县| 于都县| 米泉市| 安徽省| 姜堰市|