1222 1

[数据] 数据采集:频道采集&新闻采集 [推广有奖]

企业贵宾

巨擘

0%

还不是VIP/贵宾

-

威望
4
论坛币
624047 个
通用积分
147.0356
学术水平
918 点
热心指数
988 点
信用等级
842 点
经验
398722 点
帖子
9795
精华
48
在线时间
17322 小时
注册时间
2014-8-19
最后登录
2022-11-2

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币

数据采集:频道采集&新闻采集


代码:package controllers;


import java.util.ArrayList;

import java.util.Iterator;

import java.util.List;


import org.h2.mvstore.DataUtils;


import com.avaje.ebean.Ebean;

import com.avaje.ebean.Page;

import com.fasterxml.jackson.databind.JsonNode;

import com.fasterxml.jackson.databind.node.ArrayNode;

import com.yhpl.utils.CollectionUtil;

import com.yhpl.utils.DateUtil;

import com.yhpl.utils.JsonFileUtil;

import com.yhpl.utils.NewsUrlUtil;

import com.yhpl.utils.work.TrimFilter;

import com.yhpl.vo.NewChannalVo;

import com.yhpl.vo.NewChannalsVo;

import com.yhpl.vo.NewsVo;


import models.Channels;

import models.News;

import play.libs.Json;

import play.mvc.Controller;

import play.mvc.Result;


public class CaptureController extends Controller {


        public static Result capture() {

                return ok("111");

        }


        public static Result initChannels() {

                NewChannalsVo chanals = (NewChannalsVo) JsonFileUtil.getGetUrlContentAsObject(NewsUrlUtil.getChannelUrl(),

                                NewChannalsVo.class);

                if (chanals != null) {

                        NewChannalVo[] channelArray = chanals.gettList();

                        List<Channels> beans = new ArrayList<Channels>();

                        for (int i = 0; i < channelArray.length; i++) {

                                NewChannalVo jsonObj = channelArray;

                                Channels bean = new Channels();

                                bean.cname = jsonObj.getTname();

                                bean.cid = jsonObj.getTid();

                                bean.subnum = jsonObj.getSubnum();

                                beans.add(bean);


                        }

                        Ebean.beginTransaction();

                        for (int i = 0; i < channelArray.length; i++) {

                                Channels bean = beans.get(i);

                                Channels target = Channels.getChannelWithCname(bean.cname);

                                if (target == null) {

                                        Ebean.save(bean);

                                } else {

                                        System.out.println("exist -- " + target.cname);

                                }

                        }


                        Ebean.commitTransaction();


                }

                return ok("init Channels success");

        }


        public static Result initNews() {

                Page<Channels> pageChannel = Channels.page(1, 20, "id", "asc");

                if (pageChannel.getTotalRowCount() > 0) {

                        List<Channels> channelBeans = pageChannel.getList();

                        if (!CollectionUtil.isEmpty(channelBeans)) {

                                for (int i = 0; i < channelBeans.size(); i++) {


                                        // A.解析数据

                                        Channels channelBean = channelBeans.get(i);

                                        String url = NewsUrlUtil.getChannelNewsUrlWithCidPageCount(channelBean.cid);

                                        JsonNode node = JsonFileUtil.getGetUrlContentAsJsonNode(url);

                                        ArrayNode arrayNodes = (ArrayNode) node.get(channelBean.cid);

                                        Iterator<JsonNode> iter = arrayNodes.iterator();

                                        List<News> mNews = new ArrayList<News>();

                                        while (iter.hasNext()) {

                                                JsonNode childNode = iter.next();

                                                NewsVo childNews = Json.fromJson(childNode, NewsVo.class);

                                                News news = new News();

                                                news.cid = channelBean.cid;

                                                news.cp = childNews.getSource();

                                                news.icon = childNews.getImgsrc();

                                                news.url = childNews.getUrl();

                                                news.title = childNews.getTitle();

                                                news.snapDetail = childNews.getDigest();

                                                news.time = DateUtil.getDateFromString(childNews.getPtime());

                                                mNews.add(news);

                                                System.out.println("child:" + childNews);

                                        }


                                        // B.过滤数据库

                                        CollectionUtil.trimListWithFilter(mNews, new TrimFilter<News>() {


                                                @Override

                                                public boolean isFilter(News t) {

                                                        return t != null && News.getNewsWithTitle(t.title) != null;

                                                }

                                        });


                                        // C.POJO -> DB

                                        if (!CollectionUtil.isEmpty(mNews)) {

                                                System.out.println("save --- > :" + channelBean.cid);

                                                Ebean.save(mNews);

                                        }


                                }

                        }

                }


                return ok("init News success");

        }


        public static Result page(String table) {


                if (News.T_NAME.equals(table)) {

                        Page<News> news = News.page(1, 10, "time", "desc", "netease");

                        int count = news.getTotalPageCount();

                        System.out.println("count is " + count);

                        List<News> mList = news.getList();

                        return ok(Json.toJson(mList));

                } else {

                        return ok("not found");

                }


        }


}


1.频道采集:


public static Result initChannels() {

        NewChannalsVo chanals = (NewChannalsVo) JsonFileUtil.getGetUrlContentAsObject(NewsUrlUtil.getChannelUrl(),

                NewChannalsVo.class);

        if (chanals != null) {

            NewChannalVo[] channelArray = chanals.gettList();

            List<Channels> beans = new ArrayList<Channels>();

            for (int i = 0; i < channelArray.length; i++) {

                NewChannalVo jsonObj = channelArray;

                Channels bean = new Channels();

                bean.cname = jsonObj.getTname();

                bean.cid = jsonObj.getTid();

                bean.subnum = jsonObj.getSubnum();

                beans.add(bean);

            }

            Ebean.beginTransaction();

            for (int i = 0; i < channelArray.length; i++) {

                Channels bean = beans.get(i);

                Channels target = Channels.getChannelWithCname(bean.cname);

                if (target == null) {

                    Ebean.save(bean);

                } else {

                    System.out.println("exist -- " + target.cname);

                }

            }

            Ebean.commitTransaction();

        }

        return ok("init Channels success");

    }



        2.新闻采集:


public static Result initNews() {

        Page<Channels> pageChannel = Channels.page(1, 20, "id", "asc");

        if (pageChannel.getTotalRowCount() > 0) {

            List<Channels> channelBeans = pageChannel.getList();

            if (!CollectionUtil.isEmpty(channelBeans)) {

                for (int i = 0; i < channelBeans.size(); i++) {

                    // A.解析数据

                    Channels channelBean = channelBeans.get(i);

                    String url = NewsUrlUtil.getChannelNewsUrlWithCidPageCount(channelBean.cid);

                    JsonNode node = JsonFileUtil.getGetUrlContentAsJsonNode(url);

                    ArrayNode arrayNodes = (ArrayNode) node.get(channelBean.cid);

                    Iterator<JsonNode> iter = arrayNodes.iterator();

                    List<News> mNews = new ArrayList<News>();

                    while (iter.hasNext()) {

                        JsonNode childNode = iter.next();

                        NewsVo childNews = Json.fromJson(childNode, NewsVo.class);

                        News news = new News();

                        news.cid = channelBean.cid;

                        news.cp = childNews.getSource();

                        news.icon = childNews.getImgsrc();

                        news.url = childNews.getUrl();

                        news.title = childNews.getTitle();

                        news.snapDetail = childNews.getDigest();

                        news.time = DateUtil.getDateFromString(childNews.getPtime());

                        mNews.add(news);

                        System.out.println("child:" + childNews);

                    }

                    // B.过滤数据库

                    CollectionUtil.trimListWithFilter(mNews, new TrimFilter<News>() {

                        @Override

                        public boolean isFilter(News t) {

                            return t != null && News.getNewsWithTitle(t.title) != null;

                        }

                    });

                    // C.POJO -> DB

                    if (!CollectionUtil.isEmpty(mNews)) {

                        System.out.println("save --- > :" + channelBean.cid);

                        Ebean.save(mNews);

                    }

                }

            }

        }

        return ok("init News success");

    }





               3.运行play之后,第一次初始化会要求add scripts,点击add即可。


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:数据采集 Transaction Collection controller Channels 数据采集


https://www.cda.cn/?seo-luntan
高薪就业·数据科学人才·16年教育品牌
沙发
涂中归 学生认证  发表于 2015-10-31 15:36:27 |只看作者 |坛友微信交流群
好东西 谢谢楼主

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-4-30 19:37