Small Asteroid Blog

[Java] 구글 뉴스 날짜별로 크롤링하기 본문

백엔드/Spring

[Java] 구글 뉴스 날짜별로 크롤링하기

작은소행성☄️ 2021. 11. 8. 20:38
728x90

구글에서 날짜별로 크롤링을 하고자 한다.

 

 

 

duration에 적혀있는 날짜는 원하는 날짜로 변경해서 사용하면된다. 

구글에서 날짜기준을 적을 때 1/1/2020 이러한 포맷으로 사용해야한다.

아래에 굵은 글씨 부분의 숫자를 원하는 날짜로 변경해서 사용하면 된다. 

"%2Ccd_min%3A1%2F1%2F2020%2Ccd_max%3A1%2F31%2F2020"

 

 

 //google
    @RequestMapping("googlejsoup")
    public String googlejsoup(Model model, @RequestParam String keyword){

        String apiUrl;
        Document doc = null;

        ArrayList<String> al1 = new ArrayList<>();
        ArrayList<String> al2 = new ArrayList<>();

        String href;
        Elements div1;
        Elements span1;
        String duration;

        for(int i=0;i<=400;i+=20){
            doc = null;

//            duration = "%2Ccd_min%3A1%2F1%2F2020%2Ccd_max%3A12%2F31%2F2020"; //20200101-20201231
            duration = "%2Ccd_min%3A1%2F1%2F2020%2Ccd_max%3A1%2F31%2F2020"; //20200101-20200131
          
            apiUrl ="https://www.google.com/search?q="+keyword+"&biw=1005&bih=842&source=lnt&tbs=cdr%3A1"+duration+"&tbm=nws"
                    +"&start="+i;

            try {
                doc = Jsoup.connect(apiUrl).get();

                //기사 제목
                Elements elements1 = doc.select(".WlydOe");
                Elements title = doc.select("div[class=mCBkyc tNxQIb ynAwRc JIFdL JQe2Ld nDgy9d]");
                for(Element el1:title){
                    al1.add(el1.ownText());
                }

                Elements content = doc.select("div[class=GI74Re nDgy9d]");
                for(Element el:content){
                    al2.add(el.ownText());
                }

                int index = 0;
                for(Element el:elements1){
                    System.out.println("title > "+al1.get(index));
                    System.out.println("content > "+al2.get(index));

                    href = el.select("a").attr("href");
                    System.out.println("href "+href);

                    div1 = el.select("div");
                    Element div2= div1.first();
                    System.out.println("div2 text >"+div2.text());

                    span1 = el.select("span");
                    Element agency= span1.first();
                    System.out.println("agency text >"+agency.text()+" , len > "+agency.text().length());

                    String main = div2.text().substring(agency.text().length(),div2.text().length()-12);
                    System.out.println("div main >> "+main);

                    String pubdate = div2.text().substring(div2.text().length()-13);
                    System.out.println("date >> "+pubdate);


                    //db저장
//                    Source source = new Source();
//                    source.setTitle(al1.get(index));
//                    source.setLink(href);
//                    source.setContent(al2.get(index));
//                    source.setDate(pubdate);
//                    source.setSearchword(keyword);
//                    source.setSite(agency.text());
//                    source.setType("뉴스");
//                    newsSaveService.save1(source);

                    index++;
                }
            }catch (Exception e){
                System.out.println(e);
            }
        }

        return "jsoupsearch";
    }

 

 

 

 

728x90
반응형