吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1430|回复: 11
收起左侧

[Java 原创] Java HTML解析器

[复制链接]
henry307 发表于 2023-3-23 12:54
本帖最后由 henry307 于 2023-3-23 12:58 编辑

这是一个对HTML进行分析的快速实时的解析器,可以通过DOM或CSS选择器来查找,提取数据。下面例子展示此解析器的用法,例子还用到了上文提到的 Java爬虫引擎
[Java] 纯文本查看 复制代码
package cfw.test;

import cfw.html.TagSearchRange;
import cfw.html.HtmlParser;
import cfw.html.HtmlTag;
import cfw.http.ResponseResult;
import cfw.http.UserAgentPack;
import cfw.http.WebClient;
import cfw.http.WebRequest;
import cfw.model.FinancialNewsListModel;
import com.alibaba.fastjson.JSONArray;
import com.sun.jndi.toolkit.url.Uri;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class HtmlParserTest {


    public static void main(String[] args) {

        // 【新浪财经要闻文章列表】抓取
        List<FinancialNewsListModel> sinaNews = crawlSinaFinancialNewsList();
        System.out.println(String.format("【新浪财经-要闻列表页】抓取到%s个文章列表", sinaNews.size()));
        String json  = JSONArray.toJSON(sinaNews).toString();
        System.out.println(json);

        // 【凤凰财经文章列表】抓取

        List<FinancialNewsListModel> ifengNews = crawlIFengFinancialNewsList();
        System.out.println(String.format("【凤凰财经-文章列表页】抓取到%s个文章列表", ifengNews.size()));
        String json2 = JSONArray.toJSON(ifengNews).toString();
        System.out.println(json2);

    }


    /**
     * 【新浪财经要闻文章列表】抓取
     *
     * @return
     */
    private static List<FinancialNewsListModel> crawlSinaFinancialNewsList() {
            /*
             抓取地址: http://finance.sina.com.cn/
             */

        List<FinancialNewsListModel> fnews = new ArrayList<FinancialNewsListModel>();
        try {
            String url = "http://finance.sina.com.cn/";
            WebRequest req = new WebRequest();
            req.setUrl(url);
            req.setMethod("GET");
            req.setUserAgent(UserAgentPack.getUserAgentRandom());
            Map<String, String> dic = new HashMap<String, String>();
            dic.put("Upgrade-Insecure-Requests", "1");
            dic.put("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
            dic.put("Accept-Encoding", "gzip, deflate");
            dic.put("Accept-Language", "zh-CN,zh;q=0.8,en;q=0.6");
            req.setSpecialHeadCollection(dic);
            ResponseResult sr = WebClient.download(req);
            String html = sr.getResponseHtmlStr();
            HtmlParser parser = new HtmlParser(html);
            HtmlTag htmlTag = parser.parse();

            // 要闻
            HtmlTag finTag = htmlTag.getElementById("fin_tabs0_c0");
            List<HtmlTag> aTags = finTag.getElementsByTagName("a");
            for (HtmlTag aTag : aTags) {
                FinancialNewsListModel list = new FinancialNewsListModel();
                list.setArticalUrl(aTag.getAttribute("href"));
                list.setArticalTitle(aTag.getValue());
                Uri uri = new Uri(list.getArticalUrl());
                String[] strs = uri.getPath().split("/");
                if (strs.length > 1) {
                    list.setArticalMD("sina-" + strs[strs.length - 1].replace(".html", "").replace(".shtml", ""));
                    fnews.add(list);
                }
            }
        } catch (Exception ex) {
            System.out.println("【新浪财经-要闻列表页】抓取失败" + ex.getMessage());
        }

        return fnews;
    }


    /**
     * 【凤凰财经文章列表】抓取
     *
     * @return
     */
    private static List<FinancialNewsListModel> crawlIFengFinancialNewsList() {
            /*
             抓取地址: http://finance.ifeng.com/    
             */

        List<FinancialNewsListModel> fnews = new ArrayList<>();
        try {
            String url = "http://finance.ifeng.com/";
            WebRequest req = new WebRequest();
            req.setUrl(url);
            req.setMethod("GET");
            req.setUserAgent(UserAgentPack.getUserAgentRandom());
            Map<String, String> dic = new HashMap<>();
            dic.put("Upgrade-Insecure-Requests", "1");
            dic.put("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
            dic.put("Accept-Encoding", "gzip, deflate");
            dic.put("Accept-Language", "zh-CN,zh;q=0.8,en;q=0.6");
            req.setSpecialHeadCollection(dic);
            ResponseResult sr = WebClient.download(req);
            String html = sr.getResponseHtmlStr();
            HtmlParser parser = new HtmlParser(html);

            HtmlTag htmlTag = parser.parse();

            HtmlTag listTag = htmlTag.search(t -> t.getAttribute("class").equals("list-tab"), TagSearchRange.AllElements).get(0);
            List<HtmlTag> divTags = listTag.search(s -> s.getAttribute("class").equals("list_L z20 clearfix"), TagSearchRange.AllElements);
            for (HtmlTag divTag : divTags) {
                HtmlTag list_textTag = divTag.search(d -> d.getAttribute("class").equals("list_text"), TagSearchRange.AllElements).get(0);
                HtmlTag aTag = list_textTag.getElementsByTagName("a").get(0);

                FinancialNewsListModel list = new FinancialNewsListModel();
                list.setArticalUrl(aTag.getAttribute("href"));
                list.setArticalTitle(aTag.getValue());
                Uri uri = new Uri(list.getArticalUrl());
                String[] strs = uri.getPath().split("/");
                list.setArticalMD("ifeng-" + strs[strs.length - 1].replace(".html", "").replace(".shtml", ""));
                fnews.add(list);
            }


        } catch (Exception ex) {
            System.out.println("【凤凰财经-文章列表页】抓取失败" + ex.getMessage());
        }

        return fnews;
    }

}
运行结果如下: image.png


最后源码下载。

免费评分

参与人数 2吾爱币 +2 热心值 +2 收起 理由
Island96 + 1 + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!
jianghuai + 1 + 1 我很赞同!

查看全部评分

本帖被以下淘专辑推荐:

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

pengze 发表于 2023-3-23 14:55
你这个看着有点像webmagic,一般来说用Jsoup这个jar包足够了,单纯讨论下,没别的意思
Island96 发表于 2023-7-5 10:01
henry307 发表于 2023-7-5 09:50
好的,晚点我看下

import cfw.html.TagSearchRange;
import cfw.html.HtmlParser;
import cfw.html.HtmlTag
import cfw.http.UserAgentPack;
import cfw.model.FinancialNewsListModel;
您链接的那一版爬虫引擎少了这五个类
syh315 发表于 2023-3-23 16:53
 楼主| henry307 发表于 2023-3-23 19:07
pengze 发表于 2023-3-23 14:55
你这个看着有点像webmagic,一般来说用Jsoup这个jar包足够了,单纯讨论下,没别的意思

习惯了document,所以实现了方便用
huaxincanmeng 发表于 2023-3-30 15:43
jsoup也可以
 楼主| henry307 发表于 2023-3-30 21:34

这个性能更高
头像被屏蔽
jianghuai 发表于 2023-3-31 10:42
提示: 作者被禁止或删除 内容自动屏蔽
Island96 发表于 2023-7-5 09:39
大佬,你的源码下载好像没有链接哎,前一版大爬虫引擎少了一些东西
 楼主| henry307 发表于 2023-7-5 09:50
Island96 发表于 2023-7-5 09:39
大佬,你的源码下载好像没有链接哎,前一版大爬虫引擎少了一些东西

好的,晚点我看下
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-6-5 13:05

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表