Node.js puppeteer-下载并打开.idx文件

时间:2018-08-31 17:24:55

标签: node.js puppeteer

Node.js puppeteer-下载并打开.idx文件

我正在使用node.js和puppeteer来获取一些数据。我可以单击/下载.idx文件...但是如何打开它并处理数据?

const tableRows = await page.$$('table > tbody tr');
console.log(tableRows.length);
let tableCell01;
let tableCell01Val;

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.href, tableCell01 );

  const tableLink = await page.$('table > tbody tr td:nth-child(1) a');
  const tableLinkVal = await page.evaluate( tableLink => tableLink.href, tableLink );

  console.log(tableLinkVal);
  await page.goto(tableLinkVal, {waitUntil: 'load'});
}

我可以看到它已下载...但是随后出现此错误

Error: net::ERR_ABORTED at /file.20180702.idx
at navigate (/node_modules/puppeteer/lib/Page.js:602:37)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7)

2 个答案:

答案 0 :(得分:0)

尚不支持下载文件...

https://github.com/GoogleChrome/puppeteer/issues/299

您可以使用node.js可用的其他文件下载方法,以测试您的下载。

内置于Node.js:

How to download a file with Node.js (without using third-party libraries)?

模块:Axios,请求

Downloading images with node.js

答案 1 :(得分:0)

处理错误对我有用。

    /*
        https://stackoverflow.com/questions/46919013/puppeteer-wait-n-seconds-before-continuing-next-line#46965281
        https://duckduckgo.com/?q=puppeteer+await+3+seconds&atb=v110-5_b&ia=qa
    */
    function delay(time) {
        return new Promise(function(resolve) {
            setTimeout(resolve, time)
        })
    }

    await page.goto(href).catch(function(err){
        console.log('Ignore error')
    }) 

    await delay(1000)
    var tried = 0
    var bail = 10
    var downloaded = fs.existsSync(filename)
    while ( downloaded==false && tried < bail ) {
        tried++
        await delay(1000)
        console.log("fs.existsSync('" + filename +  "')")
        downloaded = fs.existsSync(filename)
    }
    if ( downloaded ) {
        console.log('Downloaded: ' + href + ' filename: ' + filename) ; 
        rename_file(filename,'../sabai/questions_tags.csv')
    } else {
        console.log('Not downloaded: ' + href + ' filename: ' + filename) ;             
    }

example log extract

Downloading: http://localhost:8000/wp-admin/admin.php?page=sabai/questions/tags&file=questions_tags-20190209.csv&q=%2Fquestions%2Ftags%2Fexport%2Fdownload filename: ../sabai/questions_tags-20190209.csv
Downloaded: http://localhost:8000/wp-admin/admin.php?page=sabai/questions/tags&file=questions_tags-20190209.csv&q=%2Fquestions%2Ftags%2Fexport%2Fdownload filename: ../sabai/questions_tags-20190209.csv

I tried using the built-in method for node, axios and requests as suggested in [Downloading files is not supported... yet.][1] but the file was never downloaded.


  [1]: https://stackoverflow.com/a/52120359/162358