简体   繁体   English

尝试使用 puppeteer 插件,但我不断收到 Unhandled Promise Rejection: ReferenceError: Can't find variable: require

[英]Trying to use puppeteer plugin but I keep getting an error for Unhandled Promise Rejection: ReferenceError: Can't find variable: require

my program is supposed to search a website named suplexed.com, for a given sneaker code, and return the price of the smallest size available for a given sneaker.我的程序应该为给定的运动鞋代码搜索名为 suplexed.com 的网站,并返回给定运动鞋可用的最小尺寸的价格。 When I run the individual.js file in the console using node I do get the value I'm looking for.当我使用节点在控制台中运行 individual.js 文件时,我确实得到了我正在寻找的值。 However, when I run HTML file that is supposed to use the.js file I get the following error但是,当我运行应该使用 .js 文件的 HTML 文件时,出现以下错误

Unhandled Promise Rejection: ReferenceError: Can't find variable: require

here is my javascript code-named "scrapers.js".这是我的 javascript 代号“scrapers.js”。 I added comments for better understanding.我添加了评论以便更好地理解。 When running the.js file in the console using node there are no problems.使用节点在控制台中运行 .js 文件时没有问题。 But when running the directory using live-server through HTML it doesn't work.但是,当通过 HTML 使用实时服务器运行目录时,它不起作用。 The problem here lies in the first few lines of code as it won't go past there这里的问题在于前几行代码,因为它不会 go 过去

async function scrapeStockx(url){
  const site = "stockx.com";
  let puppeteer = require('puppeteer-extra');
  let pluginStealth = require('puppeteer-extra-plugin-stealth');
  puppeteer.use(pluginStealth());

  puppeteer = require('puppeteer');
  
  const browser = await puppeteer.launch({headless: false})
  const page = await browser.newPage();
  await page.goto(url,{waitUntil: 'networkidle0'});


  // get the value for the row sneaker size for stockx
  await page.waitForXPath('//*[@id="pricer_table"]/tbody/tr[4]/td[1]');
  const [sneakerSize]= await page.$x('//*[@id="pricer_table"]/tbody/tr[4]/td[1]');
  
  const sneakerSizeTxt = await sneakerSize.getProperty('textContent');
  const sneakerSizeRawTxt = await sneakerSizeTxt.jsonValue(); 


  // get the value for the row sneaker size price for stockx
  await page.waitForXPath('//*[@id="pricer_table"]/tbody/tr[4]/td[2]/span[1]');
  const [sneakerPrice]= await page.$x('//*[@id="pricer_table"]/tbody/tr[4]/td[2]/span[1]');
   
  const sneakerPriceTxt = await sneakerPrice.getProperty('textContent');
  const sneakerPriceRawTxt = await sneakerPriceTxt.jsonValue(); 
  
  // print the sneaker size and price to verify TO CONSOLE
  console.log(printSizeAndPrices(sneakerSizeRawTxt, sneakerPriceRawTxt, site)); 

  // print the sneaker size and price TO WEBSITE
  let sneakerPrint = document.createElement("p");
  sneakerPrint.textContent = printSizeAndPrice(sneakerPriceRawTxt, sneakerPriceRawTxt, site)
  document.querySelector("body").appendChild(sneakerPrint);

  browser.close();
}

document.querySelector("#search-form").addEventListener("submit", function(e){
  e.preventDefault()

  // get sneaker code using the textbox created in html
  // const sneakerCode = e.target.elements.enterBox.value
  
  // manually enter a unique sneaker code
  const sneakerCode = "XXXXXX-XXX";

  // print sneaker code to console to verify
  console.log("sneakerCode: " + sneakerCode)
 
  // create the website address of the sneaker
  const address = ('https://suplexed.com/web/' + sneakerCode)

  // print website address to verify
  console.log("address: " + address)

  // call the function to retrieve the wanted values for the chosen sneaker
  scrapeStockx(address)
})

function printSizeAndPrice(sneakerSizeRawTxt, sneakerPriceRawTxt, site){
  return `${sneakerSizeRawTxt} is ${sneakerPriceRawTxt} on ${site}`;
}

Here is my html code这是我的 html 代码

<!DOCTYPE html>
<html>
    <head>
    
    </head>
    <body>
        <p>Enter a sneaker code and hit the button to search</p>

        <form id = "search-form">
            <input type = "text" placeholder = "XXXXXX-XXX" name = "enterBox">
            <button>Search</button>
        </form>

        <script src = "scrapers.js"></script>
    </body>
</html>

This isn't a complete solution to the problem, but it did bring me to a new error to which I am now researching.这不是问题的完整解决方案,但它确实给我带来了一个我现在正在研究的新错误。 In my index.html file I simply added a script tag right before my main js tag that included the require.js file在我的 index.html 文件中,我只是在包含 require.js 文件的主 js 标记之前添加了一个脚本标记

<script src = "require.js"></script>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未处理的承诺拒绝:ReferenceError:找不到变量:indexedDB - Unhandled promise rejection: ReferenceError: Can't find variable: indexedDB 未处理 Promise 拒绝:ReferenceError:找不到变量:IntersectionObserver - Unhandled Promise Rejection: ReferenceError: Can't find variable: IntersectionObserver [未处理的 promise 拒绝:ReferenceError:找不到变量:auth] - [Unhandled promise rejection: ReferenceError: Can't find variable: auth] 可能的未处理承诺拒绝(id:0):ReferenceError:找不到变量:错误 - Possible Unhandled Promise Rejection (id: 0): ReferenceError: Can't find variable: error 自定义字体 React-Native,[未处理的承诺拒绝:ReferenceError:找不到变量:ExpoFontLoader] - Custom font React-Native, [Unhandled promise rejection: ReferenceError: Can't find variable: ExpoFontLoader] 我不断收到未处理的承诺拒绝 - i keep getting unhandled promise rejection 可能未处理的 Promise 拒绝:找不到变量 ErrorTitle - Possible Unhandled Promise Rejection: Can't find variable ErrorTitle 获取参考错误:找不到变量:需要 - Getting a ReferenceError: Can't find variable: require 我收到未处理的承诺拒绝错误,但不知道为什么 - I'm getting an Unhandled Promise Rejection error but can't figure out why Puppeteer -Getting UnhandledPromiseRejectionWarning:管道中未处理的 promise 拒绝 - Puppeteer -Getting UnhandledPromiseRejectionWarning: Unhandled promise rejection in Pipelines
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM