java抓取薅羊毛网站数据 并通过服务号推送到微信

       抓取网址为 https://ym.today/yangmao

       1. java解析html首先引入jsoup

        <dependency>
            <groupId>org.jsoup</groupId>
            <artifactId>jsoup</artifactId>
            <version>1.11.3</version>
        </dependency>

       2.获取羊毛网站的html信息,然后对其节点进行解析获取数据,默认前10条数据为最新消息(也可以根据发布日期来过滤获取今日信息)

    Connection.Response response = null;
        try {
            response = Jsoup
                    .connect(YM_URL)
                    .execute();
        } catch (IOException e) {
            e.printStackTrace();
        }
        String html = response.body();

        StringBuilder sb = new StringBuilder();
        Document document = Jsoup.parse(html);

        //像js一样,通过标签获取title
        Elements postLists = document.getElementsByClass("archive-row");
        for (Element postList : postLists) {
            Elements postItems = postList.getElementsByClass("post-info");
            int i = 1;
            for (Element postItem : postItems) {
                if (i > 10)
                    break;
                Elements a = postItem.getElementsByTag("a");
                Elements b2timeago = postItem.getElementsByClass("b2timeago");
                sb.append("发布时间:")
                        .append(b2timeago.attr("datetime"))
                        .append("<br/>");
                sb.append("<a href='").append(a.attr("href")).append("'>");
                sb.append(a.text()).append("</a><br/><br/>");
                i++;
            }
        }

        3.将获取到的信息发送到微信端  这里采用的是pluspush推送加进行消息推送(每日白嫖200条信息真香~)  代码如下

  Map<String, Object> body = new HashMap<>();
        body.put("token", PLUS_PLUS_TOKEN);
        body.put("title", "今日羊毛资讯");
        body.put("content", sb.toString());
        body.put("template", "html");
        body.put("topic", null);
        HttpResponse httpResponse = HttpClientUtils.reqPost(projectService.getPushPlusUrl(), body, null);
        log.info("羊毛资讯发送响应值:{}", httpResponse.getJsonObject());

 收到消息如下

完整代码如下



private final static String PLUS_PLUS_SEND_URL = "http://www.pushplus.plus/send";
private final static String PLUS_PLUS_TOKEN = "微信关注'pushplus推送加'获取token值";
private final static String YM_URL = "https://ym.today/yangmao";


public void ym() {
        Connection.Response response = null;
        try {
            response = Jsoup
                    .connect(YM_URL)
                    .execute();
        } catch (IOException e) {
            e.printStackTrace();
        }
        String html = response.body();

        StringBuilder sb = new StringBuilder();
        Document document = Jsoup.parse(html);
        //像js一样,通过标签获取title
        Elements postLists = document.getElementsByClass("archive-row");
        for (Element postList : postLists) {
            Elements postItems = postList.getElementsByClass("post-info");
            int i = 1;
            for (Element postItem : postItems) {
                if (i > 10)
                    break;
                Elements a = postItem.getElementsByTag("a");
                Elements b2timeago = postItem.getElementsByClass("b2timeago");
                sb.append("发布时间:")
                        .append(b2timeago.attr("datetime"))
                        .append("<br/>");
                sb.append("<a href='").append(a.attr("href")).append("'>");
                sb.append(a.text()).append("</a><br/><br/>");
                i++;
            }
        }

        Map<String, Object> body = new HashMap<>();
        body.put("token", PLUS_PLUS_TOKEN);
        body.put("title", "今日羊毛资讯");
        body.put("content", sb.toString());
        body.put("template", "html");
        body.put("topic", null);
        HttpResponse httpResponse = reqPost(projectService.getPushPlusUrl(), body, null, 999);
        log.info("羊毛资讯发送响应值:{}", httpResponse.getJsonObject());

    }

 public HttpResponse reqPost(String url, Object params, Map<String, Object> headMap) {
        HttpResponse httpResponse = new HttpResponse();
        HttpClient client = new HttpClient();
        client.getHttpConnectionManager().getParams().setSoTimeout(30000);
        client.getHttpConnectionManager().getParams().setConnectionTimeout(30000);
        try {
            PostMethod method = new PostMethod(url);
            method.addRequestHeader("Content-Type", "application/json;charset=utf-8");
            if (headMap != null && headMap.size() > 0) {
                for (Map.Entry<String, Object> entry : headMap.entrySet()) {
                    method.addRequestHeader(entry.getKey(), entry.getValue().toString());
                }
            }
            if (params != null) {
                if (params instanceof List<?>) {
                    method.setRequestBody(JSONArray.toJSON(params).toString());
                } else {
                    method.setRequestBody(JSONObject.toJSON(params).toString());
                }
            }
            int httpStatus = client.executeMethod(method);
            InputStream stream = method.getResponseBodyAsStream();
            byte[] cache = new byte[1024];
            int length = -1;
            StringBuffer sb = new StringBuffer("");
            while ((length = stream.read(cache)) != -1) {
                sb.append(new String(cache, 0, length));
            }
            stream.close();
            if (httpStatus == HttpStatus.SC_OK) {
                JSONObject retJson = JSONObject.parseObject(sb.toString());
                httpResponse.setHttpStatus(httpStatus);
                httpResponse.setJsonObject(retJson);
                return httpResponse;
            } else {
                JSONObject retJson = JSONObject.parseObject(sb.toString());
                httpResponse.setHttpStatus(httpStatus);
                httpResponse.setJsonObject(retJson);
                return httpResponse;
            }
        } catch (IOException e) {
            log.error("[HttpClientUtils-reqPost]==>  url: {}, params: {},headMap: {} 异常", url, JSON.toJSONString(params), headMap);
            e.printStackTrace();
            httpResponse.setHttpStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
        }
        return httpResponse;
    }


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值