0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

NodeでURLをループしてスクレイピングするツールを作成

0
Posted at

モジュールはpuppeteerを使用

app.js
const puppeteer = require('puppeteer')
const URLS = require('./url.json')

!(async () => {
    try {
        // urlの数だけ。
        for (let i = 0; i < URLS.url.length; i++) {
            let browser = await puppeteer.launch()
            let page = await browser.newPage()

            let result = await getData(page, URLS.url[i])
            // ーーーーーgetデータ処理ーーーーーーー
            console.log(result)

            browser.close()
        }
    } catch (e) {
        console.error(e)
    }
})()

// データ取得ロジック
async function getData(page, url) {
    await page.goto(url) // ページへ移動
    let get_object = await page.evaluate(() => {
        // jsで取得データをリターン 欲しい要素をここで取る
        let data = document.getElementsByClassName('className')[0].innerHTML
        return data
    })
    return get_object
}

URLファイルのモジュールはこちら

url.json
{
    "url": [
        "https://qiita.com/",
        "https://qiita.com/timeline"
    ]
}

 参考

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?