C#了解(6.C#NAVER APIを使用してニュース情報を取得)
11188 ワード
1.NAVER APIを使用してニュース情報を取得する
01.参考ブログ
-1.NAVER APIを使用して完全な構成とXMLコードを提供する
https://ehpub.co.kr/news-crawling-library-make-csharp-data-analysis/
-2.JSONでニュースデータを受信して出力する
http://www.toughman.pe.kr/2017/05/c%EC%9D%84-%EC%9D%B4%EC%9A%A9%ED%95%9C-%EB%84%A4%EC%9D%B4%EB%B2%84-%EC%98%A4%ED%94%88-api-%EC%97%B0%EB%8F%99/
02.プロジェクト準備(コンソールプロジェクト)
-1.NAVERニュースAPI鍵を取得する
https://developers.naver.com/docs/serviceapi/search/news/news.md#%EB%89%B4%EC%8A%A4
-2.NugetでNetonsoftを使用します。Jsonインストール
-3.(オプション)API Tester
https://chrome.google.com/webstore/detail/talend-api-tester-free-ed/aejoelaoggembcahagimdiliamlcdmfm?page=1&hl=ko&itemlang=pt
03.プロジェクト
-1. News.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
namespace NewCloling
{
class News
{
public string Title { get; set; }
public string OriginalLink{get; set;}
public string Link { get; set; }
public string Description { get; set; }
public DateTime Pubdate { get; set; }
public News(string title, string originalLink, string link, string description, DateTime pubdate)
{
Title = title;
OriginalLink = originalLink;
Link = link;
Description = description;
Pubdate = pubdate;
}
public static News Make(XmlNode xn)
{
try
{
string title = Strip(xn.SelectSingleNode("title").InnerText);
string olink = Strip(xn.SelectSingleNode("originallink").InnerText);
string link = Strip(xn.SelectSingleNode("link").InnerText);
string description = Strip(xn.SelectSingleNode("description").InnerText);
DateTime pubdate = DateTime.Parse(xn.SelectSingleNode("pubDate").InnerText);
return new News(title, olink, link, description, pubdate);
}
catch
{
return null;
}
}
private static string Strip(string innerText)
{
int s = innerText.IndexOf("<");
int e = innerText.IndexOf(">");
while(s < e)
{
string b = innerText.Substring(0, s);
string a = innerText.Substring(e + 1);
innerText = b + a;
s = innerText.IndexOf("<");
e = innerText.IndexOf(">");
}
return innerText;
}
public override string ToString()
{
return Title;
}
}
}
-2. NaverNews.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Xml;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace NewCloling
{
class NaverNews
{
public string SRC { get; set; }
public string ID { get; set; }
public string Secret { get; set; }
public NaverNews(string iD, string secret)
{
this.ID = iD;
this.Secret = secret;
}
public int Find(string src)
{
this.SRC = src;
Stream stream;
string url = $"https://openapi.naver.com/v1/search/news.xml?query={SRC}&sort=date";
XmlDocument xdoc = MakeXMLDocument(url, out stream);
XmlNode node = xdoc.SelectSingleNode("rss");
XmlNode n = node.SelectSingleNode("channel");
int total = int.Parse(n.SelectSingleNode("total").InnerText);
stream.Close();
return total;
}
private XmlDocument MakeXMLDocument(string url, out Stream stream)
{
WebRequest request = null;
request = WebRequest.Create(url);
request.Headers.Add("X-Naver-Client-Id", ID);
request.Headers.Add("X-Naver-Client-Secret", Secret);
WebResponse response = request.GetResponse();
stream = response.GetResponseStream();
XmlDocument xdoc = new XmlDocument();
xdoc.Load(stream);
return xdoc;
}
public List<News> FindNewsToXML(int start, int display)
{
Stream stream;
string url = $"https://openapi.naver.com/v1/search/news.xml?query={SRC}&display={display}&start={start}&sort=date";
XmlDocument xdoc = MakeXMLDocument(url, out stream);
XmlNode node = xdoc.SelectSingleNode("rss");
XmlNode n = node.SelectSingleNode("channel");
XmlNodeList xnl = n.SelectNodes("item");
List<News> nc = new List<News>();
News news;
foreach(XmlNode xn in xnl)
{
news = News.Make(xn);
if(news == null)
{
break;
}
nc.Add(news);
}
stream.Close();
return nc;
}
private string MakeJSONDocument(string url)
{
string responseFromServer = string.Empty;
try
{
WebRequest request = WebRequest.Create(url);
request.Method = "GET";
request.ContentType = "application/json";
request.Headers["X-Naver-Client-Id"] = ID;
request.Headers["X-Naver-Client-Secret"] = Secret;
using (WebResponse response = request.GetResponse())
using (Stream dataStream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(dataStream))
responseFromServer = reader.ReadToEnd();
}
catch
{
return null;
}
return responseFromServer;
}
public List<News> FindNewsJson(int start, int display)
{
string url = $"https://openapi.naver.com/v1/search/news.json?query={SRC}&display={display}&start={start}&sort=date";
var parseJson = JObject.Parse(MakeJSONDocument(url));
// var QueryResultCount = Convert.ToInt32(parseJson["display"]);
// var TotalResultCount = Convert.ToInt32(parseJson["total"]);
List<News> news = JsonConvert.DeserializeObject<List<News>>(parseJson["items"].ToString());
return news;
}
}
}
-3. Program.cs
using System;
using System.Collections.Generic;
namespace NewCloling
{
class Program
{
static void Main(string[] args)
{
// naver api id & scret key
string id = "";
string secret = "";
NaverNews nn = new NaverNews(id, secret);
nn.SRC = "대구";
List<News> news = nn.FindNewsJson(1, 20);
foreach (News ns in news)
{
Console.WriteLine(ns.Title);
Console.WriteLine("==");
Console.WriteLine(ns.Description);
Console.WriteLine("===========================");
}
/*
int total = nn.Find("코로나");
Console.WriteLine(total);
List<News> nc = nn.FindNewsToXML(1, 20);
foreach(News ns in nc)
{
Console.WriteLine(ns.Title);
Console.WriteLine("==");
Console.WriteLine(ns.Description);
Console.WriteLine("===========================");
}
*/
}
}
}
ポスト
Reference
この問題について(C#了解(6.C#NAVER APIを使用してニュース情報を取得)), 我々は、より多くの情報をここで見つけました https://velog.io/@ansalstmd/C-알아가기-6.-C-네이버-API로-뉴스-정보-받아오기テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol