반응형

안녕하세요. 애드소프트 입니다.

 

오늘은 Asp.net mvc를 이용하여 구글 트렌드의 일별 급상승 인기 검색어를 RSS를 분석해 보여주는 기능을 작성해 보려합니다.

 

바로 시작하겠습니다.

 

1. RSSFeed로 asp.net mvc 프로젝트를 생성합니다.

2. RSSFeed 프로젝트에 Models에 RSSFeed.cs 를 추가합니다.

   

내용은 다음을 타이핑 합니다.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace RSSFeed.Models
{
    public class RSSFeed
    {
        public string Title { get; set; }
        public string ApproxTraffic { get; set; }
        public string Link { get; set; }
        public string Description { get; set; }
        public string PubDate { get; set; }
    }
}

3. RSSFeedController.cs 파일을 Controllers에 생성합니다.

내용은 다음을 타이핑 합니다.

using RSSFeed.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Xml.Linq;

namespace RSSFeed.Controllers
{
    public class RSSFeedController : Controller
    {
        // GET: RSSFeed
        public ActionResult Index()
        {
            ViewBag.URL = "https://trends.google.com/trends/trendingsearches/daily/rss?geo=KR";
            return View();
        }

        [HttpPost]
        public ActionResult Index(string RSSURL)
        {
            WebClient wclient = new WebClient();
            //한글이 깨지는 문제해결
            wclient.Encoding = Encoding.UTF8;
            string RSSData = wclient.DownloadString(RSSURL);

            //소문자를 읽을 경우 오류로 인식된다.
            XDocument xml = XDocument.Parse(RSSData);

            var RSSFeedData = from x in xml.Descendants("item")
                               select new RSSFeed
                               {
                                   Title = ((string)x.Element("title")),
                                   ApproxTraffic = x.DescendantNodes().ElementAt(3).ToString(),
                                   Link = ((string)x.Element("link")),
                                   Description = ((string)x.Element("description")),
                                   PubDate = ((string)x.Element("pubDate"))
                               };

            ViewBag.RSSFeed = RSSFeedData;
            ViewBag.URL = RSSURL;
            return View();
        }
    }
}

4. Views의 RSSFeed에 Index.cshtml을 추가 합니다.

내용은 다음을 타이핑합니다.

<h3><strong>구글 트렌드</strong> 일별 인기 급상승 검색어</h3>
        <br />
        @using (Html.BeginForm())
        {
            <input type="URL" name="RSSURL" placeholder="Enter RSS Feed URL Here..." class="form-control" value="@ViewBag.URL" style="width:85%; float: left;" />
        <input type="submit" class="btn btn-danger" style="width: 10%; float: right;"/>

            <br />
        }
        <table class="table table-hover">
                <tr>
                    <th>제목</th>
                    <th>조회수</th>
                    <th>링크</th>
                    <th>설명</th>
                    <th>게시일</th>
                </tr>
                @if (ViewBag.RSSFeed != null)
                {
                    foreach (var item in ViewBag.RSSFeed)
                    {
                        <tr>
                            <td>@item.Title</td>
                            <td>@item.ApproxTraffic</td>
                            <td><a href="@item.Link">@System.Web.HttpUtility.UrlDecode(@item.Link)</a></td>
                            <td>@item.Description</td>
                            <td>@item.PubDate</td>
                        </tr>

                    }
                }
        </table>

 

5. Views의 Shared에서 _Layout.cshtml에 메뉴를 추가 합니다.

<li class=""><a href="~/RSSFeed/Index">RSS조회</a></li>

메뉴 항목이 있다면 다음을 추가하여 링크를 만듭니다.

 

6. 프로젝트를 실행해 보시면 구글 트렌트의 일별 급상승 인기검색어에 대한 정보가 조회 됩니다.

   또한, RSS URL을 변경하여 다른 내용도 조회가 가능합니다. 

 

 

이상으로 포스팅을 마치겠습니다.

항상 행복하세요.

 

 

Addsoft life for a better life.

반응형

+ Recent posts