微博-阴阳师竞猜爬虫 java版

查面灵器喵的

 public void test() {
        String baseUrl = "https://m.weibo.cn/api/container/getIndex";
        HttpRequest httpRequest = HttpRequest.get(baseUrl).form("type", "uid").form("value", "7629946033");
        JSON json = JSONUtil.parse(httpRequest.execute().body());
        String name = UnicodeUtil.toString(json.getByPath("data.userInfo.screen_name", String.class));
        String containerId = json.getByPath("data.tabsInfo.tabs[1].containerid", String.class);
        String sinceId = json.getByPath("data.cardlistInfo.since_id", String.class);
        List<JSONArray> cardsList = new ArrayList<>();
        int i = 1;
        while (i < 2) {
            //第一条不需要sinceId
            if (1 == i) {
                httpRequest.form("containerid", containerId);
                JSON json2 = JSONUtil.parse(httpRequest.execute().body());
                Integer ok = json2.getByPath("ok", Integer.class);
                if (0 == ok) break;
                JSONArray cards = json2.getByPath("data.cards", JSONArray.class);
                cardsList.add(cards);
            } else {
                httpRequest.form("since_id", sinceId);
                JSON json3 = JSONUtil.parse(httpRequest.execute().body());
                Integer ok = json3.getByPath("ok", Integer.class);
                if (0 == ok) break;
                sinceId = json3.getByPath("data.cardlistInfo.since_id", String.class);
                JSONArray cards = json3.getByPath("data.cards", JSONArray.class);
                cardsList.add(cards);
            }
            i++;
        }
        List<Card> cardList = new ArrayList<>();
        for (JSONArray jsonArray : cardsList) {
            List<Card> cards = jsonArray.toList(Card.class);
            cardList.addAll(cards);
        }
        cardList.forEach(item -> {
            item.getMblog().format();
            System.out.println(item.getMblog().getText());
        });
    }

    @Data
    public static class Card {
        private Integer card_type;
        private Mblog mblog;
    }

    @Data
    public static class Mblog {
        private String text;
        private String created_at;
        private LocalDateTime createTime;

        private void format() {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
            this.createTime = LocalDateTime.parse(this.created_at.replace("+0800", "UTC"), formatter);
            StringBuilder sb = new StringBuilder(
                    this.text.trim().replace("<br /> <br />", "\n")
                            .replace("<br /><br />", "\n")
                            .replace("<br />", "\n")
                            .replace("阴阳师手游", ""));
            //去除<>包裹的
            int count = StrUtil.count(sb, "<");
            for (int i = 0; i < count; i++) {
                sb.delete(sb.indexOf("<"), sb.indexOf(">") + 1);
            }
            //去除##包裹的
            int count2 = StrUtil.count(sb, "#") / 2;
            for (int i = 0; i < count2; i++) {
                sb.delete(sb.indexOf("#"), sb.indexOf("#", sb.indexOf("#") + 1) + 1);
            }
            //去除完再清理一遍换行符
            if (0 == sb.indexOf("\n")) {
                sb.delete(0, 1);
            }
            if (sb.length() == sb.lastIndexOf("\n") +1) {
                sb.delete(sb.length() -1, sb.length());
            }
            this.text = sb.toString();
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值