【问题标题】:Why does this scrape return undefined?为什么这个抓取返回未定义?
【发布时间】:2020-04-11 07:36:57
【问题描述】:

当我运行此代码时,我得到未定义,但很明显,ski 和 product_id 是值形式。

我想要:

value="BTdtb4CBz3uSJ2qv"

value="adi-ss20-042"

但我得到“未定义”

class TresBien {
      async scrapeRaffleInfo() {
        // scrape the form_key and sku values
        const response = await axios(
          "https://tres-bien.com/adidas-yeezy-boost-380-mist-fx9764-ss20"
        );
        console.log("response: ", response);
        const html = await response.data;
    
        const $ = cheerio.load(html);
        const res = $('input[name="sku"]').val();
        const ans = $('input[name="form_key"]').val();
        console.log(res && ans);
      }
    }
    
    const main = async () => {
      const tb = new TresBien(
        "https://tres-bien.com/adidas-yeezy-700-v3-alvah-h67799-ss20"
      );
      let checkoutSucc = await tb.scrapeRaffleInfo();
      if (checkoutSucc) {
        Logger.logEventSuccess("Raffle successfully entered");
      }
    };
    
    main();

【问题讨论】:

  • scrapeRaffleInfo() 函数对 return 没有任何作用。
  • @Pointy 它有 console.log 记录未定义
  • 可能该 url 引用的产品不再可用。名为 "sku" 的输入可用于以下 url:https://tres-bien.com/adidas-yeezy-700-v3-alvah-h67799-ss20。您没有使用传递给main 中的TresBien 构造函数的url。

标签: javascript node.js web-scraping cheerio


【解决方案1】:

您的代码中有几处错误:

  1. 正如@Pointy 提到的,scrapeRaffleInfo() 正在返回 undefined,而您正尝试在 checkoutSucc 中使用它
  2. Tresbien 类不提供任何构造函数,但您将 url 作为参数传递给该类的构造函数。
  3. 您没有使用任何axios 库方法(例如:get()post()put()delete())。通常你需要axios.get(),但在你的代码中,你只是在使用axios()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-28
    • 1970-01-01
    • 1970-01-01
    • 2014-08-28
    • 2011-05-07
    • 1970-01-01
    • 2016-09-24
    相关资源
    最近更新 更多