资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

不使用第三方框架获取html页面某个标签的某个属性值-创新互联

大多数情况下,我们要获取页面源码等html代码中的某个标签的某个属性值时,不可能大费周章的去使用第三方框架,这就有点牛刀杀鸡的赶脚了。遇到这种情况我们可以使用简单的正则表达式来提取我们想要的数据。
例如,我要下个连续剧,大多数网站提供给我们的是很多列表,需要我们去一个一个去点击,这就很麻烦,现在,只需要我们使用http获取到页面的html字符串,然后使用正则表达式批量获取到a标签的href属性的值就可以了。
现在我们以某电影网站某雷下载为例,现在demo如下:
工具方法httpSendGet:

广昌ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为成都创新互联公司的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信: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("发送GET请求出现异常!" + e);
            e.printStackTrace();
        }
        // 使用finally块来关闭输入流
        finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
        return result;
    }

工具方法match:

public static List match(String source, String element, String attr) {
        List result = new ArrayList();
        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 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:g@tv.kaida365.com:2166/一千零一夜35.mp4, ftp://g:g@tv.kaida365.com:2166/一千零一夜34.mp4, ftp://g:g@tv.kaida365.com:2166/一千零一夜33.mp4, ftp://g:g@tv.kaida365.com:2166/一千零一夜32.mp4, ftp://g:g@tv.kaida365.com:2166/一千零一夜31.mp4, ftp://g:g@tv.kaida365.com:2166/一千零一夜30.mp4, ftp://g:g@tv.kaida365.com:2166/一千零一夜29.mp4, ftp://g:g@tv.kaida365.com:2166/一千零一夜28.mp4, ftp://g:g@tv.kaida365.com:2166/一千零一夜27.mp4, ftp://g:g@tv.kaida365.com:2166/一千零一夜26.mp4, ftp://g:g@tv.kaida365.com:2166/一千零一夜25.mp4, ftp://g:g@tv.kaida365.com:2166/一千零一夜24.mp4, ftp://g:g@tv.kaida365.com:2166/一千零一夜23.mp4, ftp://g:g@tv.kaida365.com:2166/一千零一夜22.mp4, ftp://g:g@tv.kaida365.com:2166/一千零一夜21.mp4, ftp://g:g@tv.kaida365.com:2166/一千零一夜20.mp4, ftp://g:g@tv.kaida365.com:2166/一千零一夜19.mp4, ftp://g:g@tv.kaida365.com:2166/一千零一夜18.mp4, ftp://g:g@tv.kaida365.com:2166/一千零一夜17.mp4, ftp://g:g@tv.kaida365.com:2166/一千零一夜16.mp4, ftp://g:g@tv.kaida365.com:2166/一千零一夜15.mp4, ftp://g:g@tv.kaida365.com:2166/一千零一夜14.mp4, ftp://g:g@tv.kaida365.com:2166/一千零一夜13.mp4, ftp://g:g@tv.kaida365.com:2166/一千零一夜12.mp4, ftp://g:g@tv.kaida365.com:2166/一千零一夜11.mp4, ftp://g:g@tv.kaida365.com:2166/一千零一夜10.mp4, ftp://g:g@tv.kaida365.com:2166/一千零一夜09.mp4, ftp://g:g@tv.kaida365.com:2166/一千零一夜08.mp4, ftp://g:g@tv.kaida365.com:2166/一千零一夜07.mp4, ftp://g:g@tv.kaida365.com:2166/一千零一夜06.mp4, ftp://g:g@tv.kaida365.com:2166/一千零一夜05.mp4, ftp://g:g@tv.kaida365.com:2166/一千零一夜04.mp4, ftp://g:g@tv.kaida365.com:2166/一千零一夜03.mp4, ftp://g:g@tv.kaida365.com:2166/一千零一夜02.mp4, ftp://g:g@tv.kaida365.com: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]

不想自己写正则表达式,可以使用第三方爬虫框架,这方面网上找很多的,我就不写了。

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


网页名称:不使用第三方框架获取html页面某个标签的某个属性值-创新互联
本文地址:http://cdkjz.cn/article/ccppjc.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220