html页面数据抓取

package com.myhitron.jlw.forum.util;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.myhitron.jlw.core.util.DataUtil;

public class CatchHtmlUtil {

    // img标签
    private static final String IMGURL_REGEX = "(&quot;.*?&quot;|<img.*src=(.*?)[^>]*?>)";
    // src路径
    private static final String IMGSRC_REGEX = "http:\"?(.*?)(\"|>|\\s+)";

    /**
     * 获取富文本信息的的文字
     * @author xuye
     * Date: 2017年8月22日 上午10:04:10
     * @param html
     * @return
     */
    public static String catchWord(String HTMLSource) {
        if (!DataUtil.isNotEmpty(HTMLSource)) {
            return null;
        }
        String regMatchEnter = "\\s*|\t|\r|\n";
        Pattern p = Pattern.compile(regMatchEnter);
        Matcher m = p.matcher(HTMLSource);
        HTMLSource = m.replaceAll("");
        String regMatchTag = "<[^>]*>";
        Pattern p1 = Pattern.compile(regMatchTag);
        Matcher m1 = p1.matcher(HTMLSource);
        HTMLSource = m1.replaceAll("");
        HTMLSource = HTMLSource.replaceAll(regMatchTag, "");
        return HTMLSource;
    }

    /**
     * 获取img标签
     * @author xuye
     * Date: 2017年8月22日 上午10:07:58
     * @param HTML
     * @return
     */
    public static List<String> getImageUrl(String html) {
        Matcher matcher = Pattern.compile(IMGURL_REGEX).matcher(html);
        List<String> listImgUrl = new ArrayList<String>();
        while (matcher.find()) {
            listImgUrl.add(matcher.group().replaceAll("&quot;", "\""));
        }
        return listImgUrl;
    }

    /**
     *  获取ImgSrc路径
     * @author xuye
     * Date: 2017年8月22日 上午10:08:04
     * @param listImageUrl
     * @return
     */
    public static List<String> getImageSrc(List<String> listImageUrl) {
        List<String> listImgSrc = new ArrayList<String>();
        for (String image : listImageUrl) {
            Matcher matcher = Pattern.compile(IMGSRC_REGEX).matcher(image);
            while (matcher.find()) {
                listImgSrc.add(matcher.group().substring(0, matcher.group().length() - 1));
            }
        }
        return listImgSrc;
    }

    public static void main(String[] args) {
        String html = "<p style=\"text-indent:0em;margin:4px auto 0px auto;\"><br></p><img src=\"\" width=\"100%\"><p style=\"text-indent:0em;margin:4px auto 0px auto;\"></p><div style=\"margin: 30px 0px; background-repeat: no-repeat; background-position: center center; background-size: cover; height: 800px; background-image: url(&quot;http://jlw.myhitron.com/jlw-forum/img/thumb-86-1503523656437.jpg&quot;);\" class=\"image\"></div><div style=\"display: flex\"><div style=\"flex: 2;padding-left:100px;padding-right: 40px;\"><h1 style=\"margin-bottom: 20px;font-size: 30px;height:45px\" class=\"text\">将来网论坛</h1><h3 style=\"font-size: 16px;min-height: 180px;\" class=\"text\">好棒的论坛!</h3></div><p style=\"flex: 1;font-size: 16px;min-height:200px;margin: 0 40px;\" class=\"text\">赞赞赞</p></div><div style=\"display: flex;margin-top: 30px;\"><div style=\"flex: 1 1 0%; background-repeat: no-repeat; background-position: center center; background-size: cover; height: 800px; background-image: url(&quot;http://jlw.myhitron.com/jlw-forum/img/thumb-86-1503523686912.jpg&quot;);\" class=\"image\"></div><div style=\"flex: 2;margin-left: 30px\"><div style=\"background-repeat: no-repeat; background-position: center center; background-size: cover; height: 390px; margin-bottom: 20px; background-image: url(&quot;http://jlw.myhitron.com/jlw-forum/img/thumb-86-1503523688911.jpg&quot;);\" class=\"image\"></div><div style=\"background-repeat: no-repeat; background-position: center center; background-size: cover; height: 390px; background-image: url(&quot;http://jlw.myhitron.com/jlw-forum/img/thumb-86-1503523693965.jpg&quot;);\" class=\"image\"></div></div></div><div style=\"display: flex;margin-top: 30px;\"><p style=\"flex:1;font-size: 14px;margin:0 40px;min-height: 400px;\" class=\"text\">哇塞</p><p>哇塞</p><p>哇塞</p></div><div style=\"display: flex;margin: 30px 0;\"><div style=\"flex: 3 1 0%; background-repeat: no-repeat; background-position: center center; background-size: cover; height: 1200px; background-image: url(&quot;http://jlw.myhitron.com/jlw-forum/img/thumb-86-1503523713183.jpg&quot;);\" class=\"image\"></div><div style=\"flex: 1;padding:0 40px\"><p>哇塞</p><div style=\"background-repeat: no-repeat; background-position: center center; background-size: cover; height: 300px; margin: 60px 0px; background-image: url(&quot;http://jlw.myhitron.com/jlw-forum/img/thumb-86-1503523721282.jpg&quot;);\" class=\"image\"></div><p>哇塞</p></div></div>";

        //String html = "<p style=\"text-indent:0em;margin:4px auto 0px auto;\"><font style=\"font-size:20.000000;color:#000000\">yu</font></p><img src=\"http://jlw.myhitron.com/jlw-forum/headimg/thumb-87-1503759242787_750_485.jpg\" width=\"100%\"/><p style=\"text-indent:0em;margin:4px auto 0px auto;\"></p><img src=\"http://jlw.myhitron.com/jlw-forum/headimg/thumb-87-1503759251229_1280_992.jpg\" width=\"100%\"/><p style=\"text-indent:0em;margin:4px auto 0px auto;\"></p>";
        //获取文字
        System.out.println(catchWord(html));

        System.out.println("###############################################");

        List<String> imgUrl = getImageUrl(html);
        System.out.println("许晔抓图片" + imgUrl.toString());
        //获取图片src地址

        List<String> imgSrc = getImageSrc(imgUrl);
        System.out.println("许晔抓的图片" + imgSrc.toString());

    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Delphi是一种用于开发应用程序的编程语言,它也可以用来进行网页数据抓取。在Delphi中,可以使用网络组件或第三方库来实现网页数据抓取的功能。一般来说,网页数据抓取的流程包括发送HTTP请求、接收和解析服务器响应、提取所需的数据等步骤。 首先,需要使用Delphi的网络组件或第三方库发送HTTP请求,请求特定网页的内容。随后,需要处理服务器返回的响应数据,在其中找到所需的信息,并对其进行解析。常用的解析方式包括正则表达式、HTML解析器等。最后,将解析得到的数据存储下来,或者进行后续的处理和分析。 在实际操作中,可以使用Delphi的网络组件TIdHTTP来发送HTTP请求,获取网页内容。同时,可以使用TStringList等组件来处理服务器响应的文本数据,并通过正则表达式或HTML解析器获取所需的数据。另外,也可以使用第三方库如REST客户端组件来简化HTTP请求的发送和响应的处理。 需要注意的是,进行网页数据抓取时,应当遵守相关的网络伦理规范和法律法规,不得进行非法、侵权或侵犯隐私的行为。另外,网页数据抓取也可能会受到目标网站的反爬虫机制的限制,需要进行相应的技术应对。 综上所述,Delphi可以通过网络组件或第三方库实现网页数据抓取的功能,可以发送HTTP请求、接收和解析服务器响应,提取所需的数据。在进行网页数据抓取时,需要遵守相关的法律法规和伦理规范,同时也要注意目标网站的反爬虫机制。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值