javascript - Node.js puppeteer mysql - 使用 mysql 在循环内的数据库中插入获取的值

标签 javascript mysql node.js puppeteer node-mysql

我正在使用 node.js 和 puppeteer 来获取一些数据。 ...现在我想将获取的数据插入数据库中...使用 mysql。以下似乎有效......但让我感到困惑的是,在 console.log('DB insert successful.Record: '+i);总是落后,一段时间后它会停止……尽管仍有可用记录的表格。

那是我的应用:

  let tableCell01;
  let tableCell01Val;
  let tableCell02;
  let tableCell02Val;

  const tableRows = await page.$$('table.tableFile2 > tbody > tr');

  for (let i=1; i < tableRows.length; i++){

    tableRow = tableRows[i];
    tableCell01 = await tableRow.$('td:nth-child(1) a');
    tableCell01Val = await page.evaluate( tableCell01 => tableCell01.innerText, tableCell01 );
    tableCell02 = await tableRow.$('td:nth-child(2)');
    tableCell02Val = await page.evaluate( tableCell02 => tableCell02.innerText, tableCell02 );

    tableCell02ValA.replace(/(^\s+|\s+$)/g,'');

    console.log('\n');
    console.log('ID: '+tableCell01Val);
    console.log('Company: '+tableCell02Val);
    console.log('Iterator: '+i);

    const insertCompanyList = "INSERT INTO companyList ( company_name, id ) values (?,?)";

    connection.query(insertCompanyList,[tableCell02Val, tableCell01Val],function(err, rows) {
      if (err) {
        console.log(err);
      } else {
        console.log('DB insert successful. Record: '+i);
      }
    });

  }

我可以在控制台中看到:

ID: 3136
Company: Company A
Iterator: 1

ID: 3143
Company: Company B
Iterator: 2
DB insert successful. Record: 1

ID: 4497
Company: Company C
Iterator: 3

ID: 3164
Company: Company D
Iterator: 4

ID: 3219
Company: Company E
Iterator: 5

ID: 3071
Company: Company F
Iterator: 6

ID: 3184
Company: Company G
Iterator: 7
DB insert successful. Record: 2

ID: 3130
Company: Company H
Iterator: 8
DB insert successful. Record: 3
DB insert successful. Record: 4
DB insert successful. Record: 5
DB insert successful. Record: 6
DB insert successful. Record: 7
DB insert successful. Record: 8        

ID: 1844
Company: Company I
Iterator: 1

ID: 3687
Company: Company J
Iterator: 2

ID: 4514
Company: ECompany K
Iterator: 3

ID: 3635
Company: Company L
Iterator: 4

ID: 3884
Company: Company M
Iterator: 5

ID: 3482
Company: Company N
Iterator: 6
DB insert successful. Record: 1

ID: 3482
Company: Company O
Iterator: 7

ID: 1827
Company: Company P
Iterator: 8
DB insert successful. Record: 2

ID: 1827
Company: Company Q
Iterator: 9

ID: 6465
Company: Company R
Iterator: 10

ID: 0731
Company: Company S
Country: B9
Iterator: 11
No pagination!
DB insert successful. Record: 3
DB insert successful. Record: 4
DB insert successful. Record: 5
DB insert successful. Record: 6
DB insert successful. Record: 7
DB insert successful. Record: 8
DB insert successful. Record: 9
DB insert successful. Record: 10
DB insert successful. Record: 11

我错过了什么?我想我需要将连接查询放在 async.function 中?!喜欢这里:Issue inserting values in a loop (for) in the database : same value is inserted - node js / sql .?

最佳答案

只要 promisify connection.query 这样你就可以 await 它了。您发布的其他问题的链接与您的问题非常相似。

这个问题被问了一遍又一遍,因为它很难理解,但基本上 connection.query 会立即运行,跳到下一行,然后稍后(当数据库响应并且事件循环有时间处理它)function(err, rows) {} 部分运行。因此,在您的一些 pupeteer 等待(或其他异步进程)之间,它正在处理 function(err,rows){}

下一步建议:学习使用util.promisify! ( https://nodejs.org/dist/latest-v8.x/docs/api/util.html#util_util_promisify_original )

  let tableCell01;
  let tableCell01Val;
  let tableCell02;
  let tableCell02Val;

  const tableRows = await page.$$('table.tableFile2 > tbody > tr');

  for (let i=1; i < tableRows.length; i++){

    tableRow = tableRows[i];
    tableCell01 = await tableRow.$('td:nth-child(1) a');
    tableCell01Val = await page.evaluate( tableCell01 => tableCell01.innerText, tableCell01 );
    tableCell02 = await tableRow.$('td:nth-child(2)');
    tableCell02Val = await page.evaluate( tableCell02 => tableCell02.innerText, tableCell02 );

    tableCell02ValA.replace(/(^\s+|\s+$)/g,'');

    console.log('\n');
    console.log('ID: '+tableCell01Val);
    console.log('Company: '+tableCell02Val);
    console.log('Iterator: '+i);

    const insertCompanyList = "INSERT INTO companyList ( company_name, id ) values (?,?)";

    let rows = await new Promise((resolve,reject)=>{
      connection.query(insertCompanyList,[tableCell02Val, tableCell01Val],function(err, rows) {
        if (err) {
          console.log(err);
          reject(err);
        } else {
          console.log('DB insert successful. Record: '+i);
          resolve(rows);
        }
      });
    });

  }

关于javascript - Node.js puppeteer mysql - 使用 mysql 在循环内的数据库中插入获取的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52101701/

相关文章:

javascript - 将 jQuery parseXml 限制为一定数量的循环

mysql - 使用 phpMyAdmin 更新单个列中的所有行

javascript - 从ajax传递数据到node js时 undefined variable

mysql - 哪些可能的原因会导致nodejs中的mysql队列达到限制问题

使用 Array.includes() 和测试重复项目的 JavaScript 问题

Javascript - 从列表中创建数组

javascript - VueJS - 如何从父 v-for 调用子组件上的事件

mysql - 如何在 Laravel 5 中为 Yajra 数据表指定特定连接(或数据库名称)

mysql - 尝试使用 mysql 确定前 70% 的购买

javascript - 从客户端 Web 浏览器与串行端口通信。