我使用cheeriogs 庫在Google App Script 中作業:
id 庫:1ReeQ6WO8kKNxoaA_O0XEQ589cIrRvEBA9qcWpNqdOP17i47u6N9M5Xh0
使用=IMPORTXML('url','xpath')我用這個 XPATH 進行呼叫:
//div[contains(@class,'match-card') and ../../td[@class='score-time ']/a[contains(@href, 'matches')]]
這個想法是收集div那些包含@class單詞的match-card
但
他需要td鏈接到@class='score-time'并a包含@href單詞的matches
我試圖用 CHEERIOGS 找到一種方法,但它總是回傳空白,我的嘗試是:
$('tr:contains(td.score-time > a[href^="/matches/"]) > div[class^="match-card"]')
.each((index, element) => {ss.getRange(index 2, 2).setValue($(element).text().trim());});
$('tr td.score-time:contains(a[href^="/matches/"]) > div[class^="match-card"]')
.each((index, element) => {ss.getRange(index 2, 2).setValue($(element).text().trim());});
$('tr td.score-time > a[href^="/matches/"] > div[class^="match-card"]')
.each((index, element) => {ss.getRange(index 2, 2).setValue($(element).text().trim());});
我怎樣才能達到我的預期結果?
通過評論中的請求獲得更多資訊:
示例鏈接:

uj5u.com熱心網友回復:
在您的情況下,以下修改后的腳本怎么樣?
修改后的腳本:
const values = [];
$("tr.expanded").each((_, element) => {
var v1 = $(element).find('span').attr('data-value');
var v2 = $(element).find('td.score-time > a').attr('href');
if (v2) {
values.push([Utilities.formatDate(new Date(Number(v1) * 1000), Session.getScriptTimeZone(), "HH:mm"), v2]);
}
});
console.log(values);
// If you want to put the values to the Spreadsheet, please use this. `ss` from your showing script.
ss.getRange(2, 2, values.length, values[0].length).setValues(values);
結果:
當針對 的 URL 運行此腳本時https://int.soccerway.com/national/finland/suomen-cup/20212022/2nd-round/r67751/,將獲得以下結果。
[
["19:00","/matches/2022/03/27/finland/suomen-cup/oujk/roi-united/3751971/"],
["21:30","/matches/2022/03/27/finland/suomen-cup/puleward-city/tornion-pallo-47/3751969/"],
["07:00","/matches/2022/03/30/finland/suomen-cup/rastaala/valtti/3751935/"],
["07:00","/matches/2022/03/30/finland/suomen-cup/hapk/japs-ii/3751944/"],
["07:00","/matches/2022/03/30/finland/suomen-cup/japs-o35/finland-kumu-junior-team/3751949/"],
["07:00","/matches/2022/03/30/finland/suomen-cup/harjun-potku/sc-riverball/3751963/"],
["07:00","/matches/2022/03/30/finland/suomen-cup/fcs/jyvaskylan-seudun-palloseura/3751965/"],
["07:00","/matches/2022/03/30/finland/suomen-cup/barca/kuopion-elo/3751967/"],
["07:00","/matches/2022/03/30/finland/suomen-cup/pallo-kerho-37/pupa-o35/3751968/"]
]
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/437283.html
標籤:javascript google-apps-script cheerio
上一篇:Javascript-上一個按鈕
