【问题标题】:Looping only delivers the last collected value循环只提供最后收集的值
【发布时间】:2021-08-20 12:18:04
【问题描述】:

我正在尝试使用 index+row 将值放入电子表格的行中,理论上有两个收集的值,我希望一个出现在 row 10 上,另一个出现在 row 11 上,但是这没有发生,我应该如何解决这个问题?

注意:这段代码的结果将最后一个收集到的值放在row 10中,而我希望第一个值放在row 10中,第二个放在row 11中。

注2:据我了解,第一个索引是0,所以行总和变成0+10,第二个索引是1,所以行总和变成1+10.

function MenuDoPlantelTimeA() {
    const sheet = SpreadsheetApp.getActive().getSheetByName('Dados Importados Time A');
    const url = 'https://www.soccer-rating.com/Juventus-Torino/1593/';

    const contentText = UrlFetchApp.fetch(url).getContentText();
    const $ = Cheerio.load(contentText);
  
    let elements = $('a')

    elements.each((index, value) => {
        let href = $(value).attr('href')
        if (href === 'javascript:showSquad()') {
        sheet.getRange(index+10, 1).setValue($(value).text().trim())
        }
    })
}

收集的值:

Udinese Calcio
Juventus Torino

【问题讨论】:

  • 当您说but that's not happening 时,发生了什么?它们出现在其他地方还是根本不出现?
  • @CarstenLøvboAndersen 我在我的问题中添加了Noteabout 我的实际代码的结果

标签: javascript jquery google-apps-script web-scraping cheerio


【解决方案1】:

您的查询不正确。

    let elements = $('a[href="javascript:showSquad()"]')

    elements.each((index, value) => {
      sheet.getRange(index+10, 1).setValue($(value).text().trim())
    })

【讨论】:

  • 顺便更新一下this question
  • 不好意思,我平时考试的时候更新,但是忘记了!
猜你喜欢
  • 2020-04-27
  • 2019-08-01
  • 2022-01-23
  • 1970-01-01
  • 2015-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多