我有一个将RSS数据保存到电子表格中的脚本,但它仍然有缺点和问题。
我收到了以下形式的数据 标题、时间、文章链接。
我希望脚本能够根据每个文章链接中的标签或 HTML 类检索描述,这样我得到的数据是 标题、时间、描述和文章链接
例如,我想从文章链接中检索名为 entry-content 的 div 类的 description https://e-ficiencia.com/samsung-climate-solutions-acudira-cyr-2023/
我希望电子表格上得到的数据是这样的 https://docs.google.com/spreadsheets/d/1lPn7xHEEI1NknN8l9w6hu4SkQburm8s-NdAjPsPc-NM/edit#gid=0
遵循我的 Google Apps 脚本
function myFunction() {
getURLData();
}
function getURLData() {
var currentData = [];
var urltoCheck = ["https://e-ficiencia.com/feed/", "https://www.climanoticias.com/feed/all","https://www.proinstalaciones.com/actualidad/noticias?format=feed"];
for (var i = 0; i < urltoCheck.length; i++){
var ficiencaData = UrlFetchApp.fetch(urltoCheck[i]);
var xml = ficiencaData.getContentText()
let response = XmlService.parse(xml);
var root = response.getRootElement();
let channel = root.getChild('channel');
let items = channel.getChildren('item');
items.forEach(item => {
let title = item.getChild('title').getText();
let pubDateb = item.getChild('pubDate').getText();
let link = item.getChild('link').getText();
currentData.push([title,pubDateb,link])
});
}
var ss = SpreadsheetApp.getActiveSpreadsheet()
var sheet = ss.getSheetByName("Sheet1");
var currentDataRange = sheet.getRange(sheet.getLastRow() + 1, 1, currentData.length, currentData[0].length);
currentDataRange.setValues(currentData);
}```
根据现有 URL 从 HTML 元素中抓取数据涉及几个步骤。
从 HTML 元素中抓取数据的算法:
获取 HTML 内容:
解析 HTML:
找到所需的元素:
标签)。
从元素中提取数据:
getText()
等方法来获取文本内容或访问图像 URL 的 src
等特定属性。数据处理:
存储或使用数据:
处理错误:
必要时重复:
尊重网站政策:
测试和验证: