ユーザ用ツール

サイト用ツール


nodejs

文書の過去の版を表示しています。


Node.js

基本操作

インストール

dnf install -y --setopt=nodesource-nodejs.module_hotfixes=1
dnf install nodejs -y --setopt=nodesource-nodejs.module_hotfixes=1

Ubuntuの場合

sudo apt install -y nodejs npm
sudo npm install n -g // nをインストール

最新のnをインストール

sudo n stable

最初に入れたnode.jsとnpmはアンインストール

$ sudo apt purge -y nodejs npm
$ sudo apt autoremove -y

バージョン確認

node -v
npm -v

プロジェクト作成

作成したプロジェクトディレクトリの直下で

npm init -y

package.json に以下のように記載する

{
  "name": "web-scraper-nodejs",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "test": "node test.js",
    "start": "node index.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "description": "",
  "dependencies": {
    "axios": "^1.5.1",
    "cheerio": "^1.0.0-rc.12"
  }
}

ファイル入出力

// index.js
 
const axios = require('axios');
const cheerio = require('cheerio');
 
const fs = require('fs');
 
 
// ファイル読み込み
try {
    const data = fs.readFileSync("S:/Tools/scraper/amazon/list.txt", 'utf8');
    console.log(data);
} catch (error) {
    console.error('ファイルの読み込みエラー',error);
}
 
// スクレイピング対象のURL
const url = 'https://amzn.asia/d/0sZ5ssp';
 
// Slack Incoming WebhooksのURLを設定
const slackWebhookUrl = '';
 
// Axiosを使用してHTMLを取得
axios.get(url)
    .then(response => {
 
        // 取得したHTMLをCheerioでパース
        const $ = cheerio.load(response.data);
 
        const price = $('#corePrice_feature_div > div > div > span.a-price.aok-align-center > span:nth-child(2) > span.a-price-whole');
        const stock = $('#availability > span.a-size-medium.a-color-success');
        console.log(price.html());
        console.log(stock.html());
        //fs.writeFileSync('S:/Tools/scraper/amazon/output.html', price.html(), 'utf-8');
 
 
    })
    .catch(error => {
        console.error('エラー:', error);
    });
nodejs.1701937893.txt.gz · 最終更新: 2023/12/07 17:31 by mikoto