nodejs:scraping
差分
このページの2つのバージョン間の差分を表示します。
| 両方とも前のリビジョン前のリビジョン次のリビジョン | 前のリビジョン | ||
| nodejs:scraping [2023/12/12 09:26] – mikoto | nodejs:scraping [2023/12/18 20:48] (現在) – mikoto | ||
|---|---|---|---|
| 行 14: | 行 14: | ||
| const axios = require(' | const axios = require(' | ||
| const cheerio = require(' | const cheerio = require(' | ||
| - | |||
| - | // スクレイピング対象のURL | ||
| - | const url = ' | ||
| // Slack Incoming WebhooksのURLを設定 | // Slack Incoming WebhooksのURLを設定 | ||
| 行 73: | 行 70: | ||
| </ | </ | ||
| + | ==== 進行中のコード ==== | ||
| <code javascript> | <code javascript> | ||
| // index.js | // index.js | ||
| 行 87: | 行 85: | ||
| const urls = [ | const urls = [ | ||
| - | ' | + | // URLs |
| - | ' | + | |
| - | ' | + | |
| - | ' | + | |
| ]; | ]; | ||
| 行 107: | 行 102: | ||
| // Axios header | // Axios header | ||
| const headers = { | const headers = { | ||
| - | //' | + | //' |
| + | ' | ||
| } | } | ||
| 行 113: | 行 109: | ||
| async function fetchData() { | async function fetchData() { | ||
| for (const url of urls) { | for (const url of urls) { | ||
| + | let price = ''; | ||
| + | let stock = ''; | ||
| try { | try { | ||
| - | const response = await axios.get(url, | + | const response = await axios.get(url, |
| const $ = cheerio.load(response.data); | const $ = cheerio.load(response.data); | ||
| - | const price = $('# | + | |
| - | let stock = ''; | + | |
| + | if (priceElement.length > 0) { | ||
| + | price = priceElement.html().trim(); | ||
| + | } else { | ||
| + | price = ''; | ||
| + | } | ||
| + | const stockInputElement = $(' | ||
| + | if (stockInputElement.length > 0 ) { | ||
| + | | ||
| + | const valueAttribute = stockInputElement.attr(' | ||
| + | if (valueAttribute === ' | ||
| + | stock = ' | ||
| + | } else { | ||
| + | stock = ' | ||
| + | } | ||
| + | } else { | ||
| + | stock = ' | ||
| + | } | ||
| + | } else if (url.includes(' | ||
| + | // 価格を取得 | ||
| + | price = $(' | ||
| + | .find(' | ||
| - | | + | // 在庫状況を確認 |
| - | if (stockInputElement.length > 0 ) { | + | |
| - | // Value属性の値を取得 | + | |
| - | const valueAttribute = stockInputElement.attr(' | + | if (addToCartBtn |
| - | if (valueAttribute | + | stock = 'あり'; |
| - | stock = '1'; | + | } else { |
| + | stock = ' | ||
| } | } | ||
| + | } else if (url.includes(' | ||
| + | price = $('# | ||
| + | |||
| + | const addToCartBtn = $('# | ||
| + | const isDisabled = addToCartBtn.prop(' | ||
| + | if (isDisabled) { | ||
| + | stock = ' | ||
| + | } else { | ||
| + | stock = ' | ||
| + | } | ||
| + | } else if (url.includes(' | ||
| + | const priceGroupEl = $(' | ||
| + | const priceWithoutCamma = priceGroupEl.find(' | ||
| + | price = parseInt(priceWithoutCamma, | ||
| + | stock = ' | ||
| } | } | ||
| + | else if (url.includes(' | ||
| + | const itemElRight = $(' | ||
| + | const itempriceArea = itemElRight.find(' | ||
| + | const priceCurrency = itempriceArea.children(' | ||
| + | price = parseInt(priceCurrency, | ||
| + | console.log(price); | ||
| + | |||
| + | const addToCartBtnEl = itemElRight.find('# | ||
| + | if (addToCartBtnEl.length > 0) { | ||
| + | stock = ' | ||
| + | } else { | ||
| + | stock = ' | ||
| + | } | ||
| + | } else if (url.includes(' | ||
| + | const price = $(' | ||
| + | .text().replace(/ | ||
| + | |||
| + | const sellBtnEl = $(' | ||
| + | if (sellBtnEl.length > 0) { | ||
| + | stock = ' | ||
| + | } else { | ||
| + | stock = ' | ||
| + | } | ||
| + | } else { | ||
| + | price = ''; | ||
| + | stock = ''; | ||
| + | } | ||
| + | | ||
| data.push({ url, price, stock }); | data.push({ url, price, stock }); | ||
| console.log(data); | console.log(data); | ||
| 行 134: | 行 197: | ||
| } catch (error) { | } catch (error) { | ||
| console.error(' | console.error(' | ||
| + | data.push({ url, price, stock }); | ||
| } | } | ||
| } | } | ||
| + | | ||
| // 全ての非同期処理が完了した後にCSVに書き込み | // 全ての非同期処理が完了した後にCSVに書き込み | ||
| await csvWriter.writeRecords(data); | await csvWriter.writeRecords(data); | ||
nodejs/scraping.1702340777.txt.gz · 最終更新: 2023/12/12 09:26 by mikoto