- AvanzaScraper
- Usage
- createScrapeStocks
- scrapeStocks
- openListMenu
- selectActiveLists
- showMoreStocks
- extractTableData
- sleep
- dateToKey
- createQueue
- createSaveStockList
- createSavePricesToStock
- saveStockList
- savePricesToStock
- priceScraper
- fetchPriceData
- createParsePriceData
- parsePriceData
This app scrapes all the stocks from Avanzas page of stock lists. It extracts the id and linkName properties which are needed to fetch the price data later on. It also grabs the name and which list the stock belongs to.
For basic usage and scrape the default lists it is only needed to run index.js.
Factory function for the scraper.
depsObject
This is the main scrapeStocks function. It loads the page, selects what lists to take the stocks from and extracts the data from the table.
paramsObjectparams.listsToSaveArray<String> The lists we want to save (optional, default[])params.urlString Url to the page we want to scrape (optional, default'https://www.avanza.se/aktier/lista.html')params.headlessBoolean Run the browser in headless mode or not (optional, defaulttrue)params.sleepTimeNumber The time to sleep inbetween trying to load more data. Increase this if all data isn't being loaded before moving forward. (optional, default1000)
Returns Array<Object> Array with all the scraped data
Opens the menu on top of the page to select what lists of stocks that should be included in the table.
Returns void
Select what lists to scrape the data for and click the selector for that one.
Returns void
Click the "show more" button to load the remaining stocks. When there is no more stocks to load the display-property of the button is changed to "none" so we check if it's still "inline-block" and if so click it.
Returns void
Extracts the data from the table of stocks.
Returns Array<Object> all the stocks in the list
Take a timeout to wait for content to load
millisecondsNumber number of milliseconds to wait
Returns Promise<void>
Takes a two-dimensional array that looks like:
const dataArr = [
[
15151651351351, // Stringified date
432143543 // Some data
]
]and returns an object:
const output = {
"15151651351351": {
date: 15151651351351,
data: 432143543
}
}Doing it this way prevents the search algo from going full O(N^2) and instead O(2N).
Returns Object Object with the date as key
Executes an array of Promises with a maximum of simultaneous requests
tasksArray<Promise> Tasks to be performed. Must be promise-based.maxNumOfWorkersnumber Default = 8. Maximum amount of simultaneous open requests (optional, default4)
Factory function for saveStockList
depsObject
Returns Function saveStockList
Factory function for savePricesToStock.
depsObject
Returns Function savePricesToStock
This function takes an array and saves them to the database in batches.
Returns Array<Object> The original array of stocks
This function adds the price data to the stock document in the database.
paramsObject
Returns Object Reference to the database document.
This is the main function that handles the workflow of fetching the price data and save it to the stock in the db.
TODO Error handling
paramsObject
Returns void
Factory function for fetchPriceData
depsObject
Returns Function fetchPriceData
Factory function for priceScraper
depsObjectdeps.fetchPriceDataFunction Function to download data (optional, defaultthis.createFetchPriceData({}))deps.savePricesToStockFunction Database handler (optional, defaultrequire('../data/dataSaver').createSavePricesToStock({}))deps.createQueueFunction Limiting the amount of open requests (optional, defaultrequire('./helpers').createQueue)
Returns Function priceScraper
Function to fetch price, volume and owner data from Avanza.
paramsObject
Returns Array<Object> Price data for the given stock
Factory function for parcePriceData
depsObjectdeps.dateToKeyFunction Assings the date as the object key to avoid O(N^2) and instead get O(N*2) (optional, defaultrequire('../scraping/helpers').dateToKey)
Returns Function parsePriceData
This parses the data from the Avanza API to proper objects. Output object looks like:
const output = [
{
open: 123,
high: 125,
low: 121,
close: 123,
volume: 374298, // # Stocks traded
owners: 4200 // # owners on Avanza
}
]jsonObject The price data from the API response