<del id="d4fwx"><form id="d4fwx"></form></del>
      <del id="d4fwx"><form id="d4fwx"></form></del><del id="d4fwx"><form id="d4fwx"></form></del>

            <code id="d4fwx"><abbr id="d4fwx"></abbr></code>
          • 不使用第三方框架獲取html頁面某個標簽的某個屬性值-創(chuàng)新互聯

            大多數情況下,我們要獲取頁面源碼等html代碼中的某個標簽的某個屬性值時,不可能大費周章的去使用第三方框架,這就有點牛刀殺雞的趕腳了。遇到這種情況我們可以使用簡單的正則表達式來提取我們想要的數據。
            例如,我要下個連續(xù)劇,大多數網站提供給我們的是很多列表,需要我們去一個一個去點擊,這就很麻煩,現在,只需要我們使用http獲取到頁面的html字符串,然后使用正則表達式批量獲取到a標簽的href屬性的值就可以了。
            現在我們以某電影網站某雷下載為例,現在demo如下:
            工具方法httpSendGet:

            廣昌ssl適用于網站、小程序/APP、API接口等需要進行數據傳輸應用場景,ssl證書未來市場廣闊!成為成都創(chuàng)新互聯公司的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯系或者加微信:028-86922220(備注:SSL證書合作)期待與您的合作!
            public static String httpSendGet(String url, String param,String charsetName) {
                    String result = "";
                    BufferedReader in = null;
                    try {
                        String urlNameString = url + "?" + param;
                        URL realUrl = new URL(urlNameString);
                        // 打開和URL之間的連接
                        URLConnection connection = realUrl.openConnection();
                        // 設置通用的請求屬性
                        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();
                        // 定義 BufferedReader輸入流來讀取URL的響應
                        in = new BufferedReader(new InputStreamReader(
                                connection.getInputStream(), charsetName));
                        String line;
                       while ((line = in.readLine()) != null) {
                            result += line;
                        }
                    } catch (Exception e) {
                        System.out.println("發(fā)送GET請求出現異常!" + e);
                        e.printStackTrace();
                    }
                    // 使用finally塊來關閉輸入流
                    finally {
                        try {
                            if (in != null) {
                                in.close();
                            }
                        } catch (Exception e2) {
                            e2.printStackTrace();
                        }
                    }
                    return result;
                }

            工具方法match:

            public static List<String> match(String source, String element, String attr) {
                    List<String> result = new ArrayList<String>();
                    String reg = "<" + element + "[^<>]*?\\s" + attr + "=['\"]?(.*?)['\"]?(\\s.*?)?>";
                    Matcher m = Pattern.compile(reg).matcher(source);
                   while (m.find()) {
                        String r = m.group(1);
                        result.add(r);
                    }
                    return result;
                }

            調用demo:

            public static void main(String[] args) {
                    String url = "https://www.dy2018.com/i/99671.html";
                    String params = "";
                    String html = httpSendGet(url,params,"gb2312");
                    List<String> links = match(html,"a","href");
                    System.out.println(links);
            
                }

            這里需要說明一下,httpSendGet的charsetName參數需要注意,不然你獲取的html文本會是亂碼。
            最后展示一下結果(當然結果還不純粹,需要過濾):
            [/, /2/, /0/, /3/, /1/, /4/, /8/, /5/, /7/, /14/, /15/, /html/tv/hytv/index.html, /html/tv/oumeitv/index.html, /html/tv/rihantv/index.html, /html/zongyi2013/index.html, /html/dongman/index.html, /support/GuestBook.php, #, index.html, /, /html/tv/, /html/tv/hytv/, javascript:window.external.addFavorite('http://www.dy2018.com/','dy2018.com-電影天堂')"class="style11, /webPlay/play-id-99671-collection-37.html, /webPlay/play-id-99671-collection-36.html, /webPlay/play-id-99671-collection-35.html, /webPlay/play-id-99671-collection-34.html, /webPlay/play-id-99671-collection-33.html, /webPlay/play-id-99671-collection-32.html, /webPlay/play-id-99671-collection-31.html, /webPlay/play-id-99671-collection-30.html, /webPlay/play-id-99671-collection-29.html, /webPlay/play-id-99671-collection-28.html, /webPlay/play-id-99671-collection-27.html, /webPlay/play-id-99671-collection-26.html, /webPlay/play-id-99671-collection-25.html, /webPlay/play-id-99671-collection-24.html, /webPlay/play-id-99671-collection-23.html, /webPlay/play-id-99671-collection-22.html, /webPlay/play-id-99671-collection-21.html, /webPlay/play-id-99671-collection-20.html, /webPlay/play-id-99671-collection-19.html, /webPlay/play-id-99671-collection-18.html, /webPlay/play-id-99671-collection-17.html, /webPlay/play-id-99671-collection-16.html, /webPlay/play-id-99671-collection-15.html, /webPlay/play-id-99671-collection-14.html, /webPlay/play-id-99671-collection-13.html, /webPlay/play-id-99671-collection-12.html, /webPlay/play-id-99671-collection-11.html, /webPlay/play-id-99671-collection-10.html, /webPlay/play-id-99671-collection-9.html, /webPlay/play-id-99671-collection-8.html, /webPlay/play-id-99671-collection-7.html, /webPlay/play-id-99671-collection-6.html, /webPlay/play-id-99671-collection-5.html, /webPlay/play-id-99671-collection-4.html, /webPlay/play-id-99671-collection-3.html, /webPlay/play-id-99671-collection-2.html, /webPlay/play-id-99671-collection-1.html, /webPlay/play-id-99671-collection-0.html, ftp://g:[email protected]:2166/一千零一夜35.mp4, ftp://g:[email protected]:2166/一千零一夜34.mp4, ftp://g:[email protected]:2166/一千零一夜33.mp4, ftp://g:[email protected]:2166/一千零一夜32.mp4, ftp://g:[email protected]:2166/一千零一夜31.mp4, ftp://g:[email protected]:2166/一千零一夜30.mp4, ftp://g:[email protected]:2166/一千零一夜29.mp4, ftp://g:[email protected]:2166/一千零一夜28.mp4, ftp://g:[email protected]:2166/一千零一夜27.mp4, ftp://g:[email protected]:2166/一千零一夜26.mp4, ftp://g:[email protected]:2166/一千零一夜25.mp4, ftp://g:[email protected]:2166/一千零一夜24.mp4, ftp://g:[email protected]:2166/一千零一夜23.mp4, ftp://g:[email protected]:2166/一千零一夜22.mp4, ftp://g:[email protected]:2166/一千零一夜21.mp4, ftp://g:[email protected]:2166/一千零一夜20.mp4, ftp://g:[email protected]:2166/一千零一夜19.mp4, ftp://g:[email protected]:2166/一千零一夜18.mp4, ftp://g:[email protected]:2166/一千零一夜17.mp4, ftp://g:[email protected]:2166/一千零一夜16.mp4, ftp://g:[email protected]:2166/一千零一夜15.mp4, ftp://g:[email protected]:2166/一千零一夜14.mp4, ftp://g:[email protected]:2166/一千零一夜13.mp4, ftp://g:[email protected]:2166/一千零一夜12.mp4, ftp://g:[email protected]:2166/一千零一夜11.mp4, ftp://g:[email protected]:2166/一千零一夜10.mp4, ftp://g:[email protected]:2166/一千零一夜09.mp4, ftp://g:[email protected]:2166/一千零一夜08.mp4, ftp://g:[email protected]:2166/一千零一夜07.mp4, ftp://g:[email protected]:2166/一千零一夜06.mp4, ftp://g:[email protected]:2166/一千零一夜05.mp4, ftp://g:[email protected]:2166/一千零一夜04.mp4, ftp://g:[email protected]:2166/一千零一夜03.mp4, ftp://g:[email protected]:2166/一千零一夜02.mp4, ftp://g:[email protected]:2166/一千零一夜01.mp4, /i/99743.html, /i/99734.html, /i/99733.html, /i/99725.html, /i/99720.html, /i/99719.html, /i/99716.html, /i/99708.html, /i/99704.html, /i/99695.html, /i/97129.html, /i/97575.html, /i/97041.html, /i/92091.html, /i/97637.html, /i/92020.html, /i/95187.html, /i/92000.html, /i/98343.html, /i/97363.html]

            不想自己寫正則表達式,可以使用第三方爬蟲框架,這方面網上找很多的,我就不寫了。

            另外有需要云服務器可以了解下創(chuàng)新互聯scvps.cn,海內外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。

            網站名稱:不使用第三方框架獲取html頁面某個標簽的某個屬性值-創(chuàng)新互聯
            文章來源:http://www.jbt999.com/article22/ccppjc.html

            成都網站建設公司_創(chuàng)新互聯,為您提供云服務器網站設計、軟件開發(fā)品牌網站建設、定制網站網站導航

            廣告

            聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:[email protected]。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯

            搜索引擎優(yōu)化

              <del id="d4fwx"><form id="d4fwx"></form></del>
              <del id="d4fwx"><form id="d4fwx"></form></del><del id="d4fwx"><form id="d4fwx"></form></del>

                    <code id="d4fwx"><abbr id="d4fwx"></abbr></code>
                  • 夜夜撸大香蕉 | 台湾精品一区二区三区最新作品 | 日韩网站中文字幕 | 免费高潮视频 | 欧美操逼视频 青青操 |