본문 바로가기

spring/소스코드

실시간검색어,날씨 크롤링과 미디어쿼리

MovieVO

package com.example.domain;

public class MovieVO {
	private String title;
	private String rank;
	private String image;
	private String percent;
	private String link;
	
	public String getLink() {
		return link;
	}
	public void setLink(String link) {
		this.link = link;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getRank() {
		return rank;
	}
	public void setRank(String rank) {
		this.rank = rank;
	}
	public String getImage() {
		return image;
	}
	public void setImage(String image) {
		this.image = image;
	}
	public String getPercent() {
		return percent;
	}
	public void setPercent(String percent) {
		this.percent = percent;
	}
	
	
	@Override
	public String toString() {
		return "MovieVO [title=" + title + ", rank=" + rank + ", image=" + image + ", percent=" + percent + "]";
	}
	
	
	
}


TripVO

package com.example.domain;

public class TripVO {
	private String image;
	private String title;
	
	public String getImage() {
		return image;
	}
	public void setImage(String image) {
		this.image = image;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	
	
	@Override
	public String toString() {
		return "TripClass [image=" + image + ", title=" + title + "]";
	}
	
	
}

HomeController

 

package com.example.web;

import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.example.domain.MovieVO;
import com.example.domain.TripVO;

import org.jsoup.*;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
/**
 * Handles requests for the application home page.
 */
@Controller
public class HomeController {
	
	private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
	
	/**
	 * Simply selects the home view to render by returning its name.
	 */
	@RequestMapping(value = "/", method = RequestMethod.GET)
	public String home(Locale locale, Model model) {
		logger.info("Welcome home! The client locale is {}.", locale);
		
		Date date = new Date();
		DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
		
		String formattedDate = dateFormat.format(date);
		
		model.addAttribute("serverTime", formattedDate );
		
		return "home";
	}
	
	@ResponseBody
	@RequestMapping("movie.json")
	public ArrayList movie() throws Exception{
		ArrayList array=new ArrayList();
		Document doc=Jsoup.connect("http://www.cgv.co.kr/movies").get();
		Elements es=doc.select(".sect-movie-chart ol");
		for(Element e:es.select("li")){
			MovieVO vo=new MovieVO();
			vo.setRank(e.select(".rank").text());
			vo.setTitle(e.select(".title").text());
			vo.setImage(e.select("img").attr("src"));
			vo.setPercent(e.select(".percent span").text());
			vo.setLink(e.select(".box-image a").attr("href"));
			if(!e.select(".rank").text().equals("")){
				array.add(vo);
			}
		}
		return array;
	}
	
	@RequestMapping("pmovie")
	public String moviejsp(){
		return "pmovie";
	}
	//네이버 실시간 검색어 크롤링
	@ResponseBody
	@RequestMapping("naver.json")
	public ArrayList naver() throws Exception{
		ArrayList array=new ArrayList();
		Document doc=Jsoup.connect("http://www.naver.com").get();
		Elements es=doc.select(".ah_list .ah_l");
		for(Element e:es.select(".ah_item")){
			MovieVO vo=new MovieVO();
			vo.setRank(e.select(".ah_r").text());
			vo.setTitle(e.select(".ah_k").text());
			array.add(vo);
		}
		return array;
	}
	//다음 날씨 크롤링
	@ResponseBody
	@RequestMapping("daum.json")
	public HashMap<String,Object> daum() throws Exception{
		
		HashMap<String,Object> map=new HashMap<String,Object>();
		
		Document doc=Jsoup.connect("http://www.daum.net").get();
	
		Elements today=doc.select(".info_today");
		map.put("date", today.select(".date_today").text());
		Elements weather=doc.select(".list_weather");
		
		ArrayList<HashMap<String,Object>> array=new ArrayList<HashMap<String,Object>>();
		for(Element e:weather.select(".hide")){
			HashMap<String,Object> wmap=new HashMap<String,Object>();
			wmap.put("part", e.select(".txt_part").text());
			wmap.put("temper", e.select(".txt_temper").text());
			wmap.put("ico", e.select(".ico_ws").text());
			array.add(wmap);
		}
		map.put("list",array);
		return map;
	}
	//네이버 영화
	@RequestMapping("mmovie")
	public String mmoviejsp(){
		return "mmovie";
	}
	@ResponseBody
	@RequestMapping("trip.json")
	public ArrayList trip() throws Exception{
		ArrayList array=new ArrayList();
		Document doc=Jsoup.connect("http://www.naver.com").get();
		
		Elements es=doc.select(".area_themecast id_travel .themecast_list");
		for(Element e:es.select("li")){
			TripVO vo=new TripVO();
			vo.setTitle(e.select(".td_t").text());
			vo.setImage(e.select(".td_mw img").attr("src"));
			array.add(vo);
		}
		return array;
	}
}

mmovie.jsp

	
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>



	
	Mobile 버전
	
	
	
	
	

                

                
                











                
                
	
	

    ICIA 교육원

    ICIA 교육원

    pmovie.jsp

    	
    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
    
    
                                                    
                    
    
                    
                    
                    
    	
    	PC버전 무비차트
    	
    	
    		
    
                    
    
                    
                    
    
    
    
    
    
    
    
    
    
    
    
                    
                    
                                    
                    
                    
    	
    	
    	
    날씨:

    [PC버전 무비차트]