映射表子项内容与操纵up

时间:2019-04-04 04:09:51

标签: javascript puppeteer

我的目标是从不同的var obj = [ { "ston": [ {"id": "identity1", "in": "string", "out": "number", "value": 10}, {"id": "identity2", "in": "string", "out": "number", "value": 10} ], "nton": [ {"id": "identity1", "in": "number", "out": "number", "value": 20}, {"id": "identity2", "in": "number", "out": "number", "value": 30} ] } ]; const tmp = obj.map(e => Object.entries(e).map(([k, v]) => v)).flat(3) const rs = tmp.filter(e => e.out==='number' && e.in ==='string') console.log(rs)标签中提取.textContent,每个标签都位于单独的<td>中。

我认为问题出在<tr>变量之内,因为我没有检查儿童的正确变量。当前,table变量仅获取第一个data,因此<tr>使用此代码求值。但是,pricevolume没有。我认为这是一个简单的修复程序,但我无法弄清楚!

JavaScript:

turnover

HTML:

try {
    const tradingData = await page.evaluate(() => {
      let table = document.querySelector("#trading-data tbody");
      let tableData = Array.from(table.children);
      let data = tableData.map(tradeData => {
        console.log(tradeData);
        let price = tradeData.querySelector(".quoteapi-price").textContent;
        console.log(price);
        let volume = tradeData.querySelector("quoteapi-volume").textContent;
        console.log(volume);
        let turnover = tradeData.querySelector("quoteapi-value").textContent;
        console.log(turnover);
        return { price, volume, turnover };
      })
      return data;
    });

    console.log(tradingData);
  } catch (err) {
    console.log(err);
  }

例如,它应返回<table id="trading-data" class="qq_table"> <tbody> <tr class="qq_tr_border_bot"> <td>Price</td> <td class="qq_td_right quoteapi-number quoteapi-price" data-quoteapi="price">$0.105</td> </tr> <tr class="qq_tr_border_bot"> <td>Change</td> <td class="qq_td_right pos" data-quoteapi="changeSignCSS"> <span data-quoteapi="change (signed)" class="quoteapi-number quoteapi-price quoteapi-change">0.005</span> <span data-quoteapi="pctChange (pct)" class="quoteapi-number quoteapi-pct-change">(5.00%)</span> </td> </tr> <tr class="qq_tr_border_bot"> <td>Volume</td> <td class="qq_td_right quoteapi-number quoteapi-volume" data-quoteapi="volume scale=false">5,119,162</td> </tr> <tr> <td>Turnover</td> <td class="qq_td_right quoteapi-number quoteapi-value" data-quoteapi="value scale=false">$540,173</td> </tr> </tbody> </table> price="$0.11"volume="3,900,558"

1 个答案:

答案 0 :(得分:0)

仅在需要多个表或tbody时才需要map函数。在您的示例中似乎并非如此,您可以这样做:

const tradingData = await page.evaluate(() => {
    let table = document.querySelector("#trading-data tbody");
    let price = table.querySelector(".quoteapi-price").textContent;
    let volume = table.querySelector(".quoteapi-volume").textContent;
    let turnover = table.querySelector(".quoteapi-value").textContent;
    return { price, volume, turnover };
});
console.log(tradingData);