【问题标题】:How to read a File which contains keywords in Nodejs如何在Nodejs中读取包含关键字的文件
【发布时间】:2021-01-11 20:51:44
【问题描述】:

我有一个问题? 我有一个文件,每行包含一个关键字(5000), 我正在使用节点中的 Puppeteer 开发一个 Scraper 它将转到一个具有搜索栏的网站,并且在该搜索栏中我想使用该文件中的关键字进行搜索,所以请有人指导我如何完成这个?我在使用正确的工具吗?我将不胜感激。

【问题讨论】:

    标签: node.js web-scraping automation puppeteer


    【解决方案1】:

    你可以使用:

    // yourVariable is the text data.
    if(yourVariable.contains("What you are looking for.")){
    // the code
    } 
    

    如果这就是你的意思。 否则,如果您的意思是转到文本文件中的每 5000 行,您可以通过运行在异步函数中使用 nthline:

    npm i nthline --save
    

    在您的控制台中,然后:

       ;(async () => {
      const nthline = require('nthline'),
        filePath = '/path/to/100-million-rows-file',
        rowIndex = 42
     
      console.log(await nthline(rowIndex, filePath))
    })()
    

    或者如果你想检查该行是否包含数据:

    ;(async () => {
      const nthline = require('nthline'),
        filePath = '/path/to/100-million-rows-file',
        rowIndex = 42
     
      console.log(await nthline(rowIndex, filePath).contains("Data"))
    })()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-22
      • 2016-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多