NodejsでIPOリストを取得

Standard
$ mkdir ipo-feed-scraper
$ cd ipo-feed-scraper
$ touch index.js
$ npm init -y
$ npm install axios axios-cache-adapter cheerio express --save
$ vim index.js
...
$ node index.js
  running on port 8888

index.js

const axios = require('axios') //Promise based HTTP client for the browser and node.js
const cheerio = require('cheerio') //Fast, flexible & lean implementation of core jQuery designed specifically for the server.
const express = require('express') //Fast, unopinionated, minimalist web framework for node.
const { setupCache } = require('axios-cache-adapter') //Caching adapter for axios. Store request results in a configurable store to prevent unneeded network requests.
 
const cache = setupCache({
  maxAge: 60 * 60 * 1000
})
// Create `axios` instance passing the newly created `cache.adapter`
const api = axios.create({
  adapter: cache.adapter
})
 
async function getIPOFeed(){
    try{
        const siteUrl = 'https://info.finance.yahoo.co.jp/ipo/'
 
        const {data} = await api({
            method: "GET",
            url: siteUrl,
        })
 
        const $ = cheerio.load(data)
        const elemSelector = '#yjMain > div > div.ipoBrandBox'
 
        const Listing=[
            'コード',
            '業種',
            '公開株数',
            '仮条件',
            '公開価格',
            '引受証券',
            'BB期間',
            '申込期間',
            '評価',
        ]
 
        const InitalPublicOffer=[
            'コード',
            '業種',
            '公開株数',
            '公開価格',
            '初値',
            '引受証券',
            '売買単位',
            '初値騰落率',
            '注目度',
        ]
 
        const ipoArr = []
 
        $(elemSelector).each((parentIdx,parentElem)=>{
            if($(parentElem).html().includes('売買単位')){
                let keyIdx = 0
                let ipoObj={}
 
                $(parentElem).children().children().children().children().filter('.text').each((childIdx,childElem)=>{
                    let tdValue = $(childElem).text()
                    if(tdValue){
                        ipoObj[InitalPublicOffer[keyIdx]] = tdValue
                        keyIdx++
                    }
 
                })
                ipoArr.push(ipoObj)
            }else{
                let keyIdx = 0
                let ipoObj={}
 
                $(parentElem).children().children().children().children().filter('.text').each((childIdx,childElem)=>{
                    let tdValue = $(childElem).text()
                    if(tdValue){
                        ipoObj[Listing[keyIdx]] = tdValue
                        keyIdx++
                    }
 
                })
                ipoArr.push(ipoObj)
            }
        })
        return ipoArr
    } catch (err){
        console.error(err)
    }
}
 
const app = express()
 
app.get('/api/ipo-feed', async(req,res)=>{
    try{
        const ipoFeed = await getIPOFeed()
        return res.status(200).json({
            result: ipoFeed,
        })
    }catch(err){
        return res.status(500).json({
            err: err.toString(),
        })
    }
})
 
app.listen(8888,()=>{
    console.log('running on port 8888')
})

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.