- 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.
deps
Object
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.
params
Objectparams.listsToSave
Array<String> The lists we want to save (optional, default[]
)params.url
String Url to the page we want to scrape (optional, default'https://www.avanza.se/aktier/lista.html'
)params.headless
Boolean Run the browser in headless mode or not (optional, defaulttrue
)params.sleepTime
Number 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
milliseconds
Number 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
tasks
Array<Promise> Tasks to be performed. Must be promise-based.maxNumOfWorkers
number Default = 8. Maximum amount of simultaneous open requests (optional, default4
)
Factory function for saveStockList
deps
Object
Returns Function saveStockList
Factory function for savePricesToStock.
deps
Object
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.
params
Object
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
params
Object
Returns void
Factory function for fetchPriceData
deps
Object
Returns Function fetchPriceData
Factory function for priceScraper
deps
Objectdeps.fetchPriceData
Function Function to download data (optional, defaultthis.createFetchPriceData({})
)deps.savePricesToStock
Function Database handler (optional, defaultrequire('../data/dataSaver').createSavePricesToStock({})
)deps.createQueue
Function Limiting the amount of open requests (optional, defaultrequire('./helpers').createQueue
)
Returns Function priceScraper
Function to fetch price, volume and owner data from Avanza.
params
Object
Returns Array<Object> Price data for the given stock
Factory function for parcePriceData
deps
Objectdeps.dateToKey
Function 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
}
]
json
Object The price data from the API response