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

菲利普·M

我正在使用node.js和puppeteer来获取一些数据。...现在我想使用mysql将获取的数据插入数据库中。下面的方法似乎起作用了……但令我困惑的是,在console.log('DB插入成功。记录:'+ 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中?!就像这里:在数据库中的循环中插入值(问题):插入相同的值-节点js / sql

科迪G

只要保证connection.query正确即可await您发布的其他问题的链接与您的问题非常相似。

反复问这个问题,因为它很难理解,但基本上connection.query是立即运行,跳到下一行,然后再过一段时间(当数据库响应并且事件循环有时间来处理它时),function(err, rows) {}零件开始运行。因此,在您的某些pepeteer等待(或其他异步进程)之间,它正在处理function(err,rows){}

下一条建议:学会使用util.promisifyhttps://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);
        }
      });
    });

  }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用 node.js 将 JSON 对象插入到 mySQL 数据库中

Node js 加速 puppeteer html 到 pdf

如何使用Node JS从MySQL数据库获取值

在node.js中实现Puppeteer

从Node.js将Json存储到MySQL数据库中

如何在Node.js中使用Winston将日志存储到mysql数据库中

如何阅读真正大的JSON文件,插入文件的数据到如何使用Node.js MySQL数据库?

使用node.js从mysql数据库中获取图像

Node.js Puppeteer如何获取TR ID值?

Node.js puppeteer - 获取没有元素的数据

使用 node.js 将行插入 MySQL 数据库时出现 ER_PARSE_ERROR

使用 JavaScript 但不使用 Node.js 插入 MySQL 数据库

将JSON插入到Node.js中的MySQL行

Node.js puppeteer - 转换获取的 href 字符串

无法使用node.js / puppeteer上传图像文件

我如何使用Node.js(Puppeteer)监视网络活动

如何使用 puppeteer(node js) 在 gmail 中保持登录状态

使用Node.JS将JSON对象数据插入MySQL

如何将MySQL数据库的结果放在Node.js中的变量中?

使用puppeteer将网络抓取的数据上传到node.js中的Firebase云存储

使用node,mysql,express和pure js从数据库中删除一行

如何使用Node JS和Express在MySQL数据库中存储PDF

如何使用MySQL数据库在Node.js中创建登录API

使用 Node.js 和 MySQL 将数据插入表中的问题

node.js更新mysql数据库

Node.js puppeteer - 从具有不同 ElementTag 的 txt 文件中获取数据

HTML无法进入Node js puppeteer

Node.js,puppeteer和延迟加载

使用node.js批量插入MySql