2025년 3월 13일 목요일

구글 트랜드 rss 파싱하기

 

1️⃣ 패키지 설치


npm install axios fast-xml-parser

#rss-parser도 사용해 봤지만 rss-parser는 네임스페이스를 파싱하지 못한다고 한다.

2️⃣ Google Trends RSS 파싱 코드 (google-trends-parser.js)


const axios = require("axios"); const { XMLParser } = require("fast-xml-parser"); const GOOGLE_TRENDS_RSS_URL = "https://trends.google.com/trending/rss?geo=US"; async function fetchGoogleTrends() { try { // RSS 피드 가져오기 const response = await axios.get(GOOGLE_TRENDS_RSS_URL); // XML 파싱 const parser = new XMLParser({ ignoreAttributes: false, parseTagValue: true }); const feed = parser.parse(response.data); // RSS 구조 확인 const items = feed.rss.channel.item; if (!items) { console.error("❌ No items found in the RSS feed."); return; } // 트렌드 뉴스 출력 items.forEach((item, index) => { console.log(`📌 Trend ${index + 1}: ${item.title}`); console.log(` 📰 Link: ${item.link}`); console.log(` 🖼️ Image: ${item["ht:picture"]}`); console.log(` 📰 Source: ${item["ht:picture_source"]}`); // ht:news_item 처리 if (item["ht:news_item"]) { const newsItems = Array.isArray(item["ht:news_item"]) ? item["ht:news_item"] : [item["ht:news_item"]]; console.log(` 🗞️ News Articles:`); newsItems.forEach((news, idx) => { console.log(` ${idx + 1}. ${news["ht:news_item_title"]}`); console.log(` 🔗 ${news["ht:news_item_url"]}`); console.log(` 🖼️ ${news["ht:news_item_picture"]}`); console.log(` 📰 Source: ${news["ht:news_item_source"]}\n`); }); } console.log("------------------------------------------------------"); }); } catch (error) { console.error("❌ Error fetching Google Trends:", error.message); } } fetchGoogleTrends();

댓글 없음:

댓글 쓰기