Search code examples
powershellweb-scrapinginvoke-webrequest

Investing.com scrape historical data with Powershell


I was inspired by the answer here and the function get_stock_historical_data in python module investpy to write in Powershell a function that retrieves all the daily prices of a stock between two dates :

$uri = 'https://www.investing.com/instruments/HistoricalDataAjax'

$params = @{
    'curr_id'= '951481'
    'smlID'= '2081817'
    'header'= 'STOXX 50 Volatility VSTOXX EUR Historical Data'
    'st_date'= '04/13/2021'
    'end_date'= '04/13/2002'
    'interval_sec'= 'Daily'
    'sort_col'= 'date'
    'sort_ord'= 'DESC'
    'action'= 'historical_data'
}

$headers = @{
    'User-Agent' = 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:33.0) Gecko/20100101 Firefox/33.0'
    'X-Requested-With'= 'XMLHttpRequest'
    'Accept'= 'text/html'
    'Accept-Encoding'= 'gzip, deflate'}

$res = Invoke-WebRequest -Method POST -Headers $headers  -Uri $uri -Body $params -ContentType application/x-www-form-urlencoded
$res.Content > .\test.txt

My issue is that the response contains

<td colspan="7" class="arial_11 blueFont center">No results found</td>

But it should return all the prices of the stock over a year

For information, when I send it directly from investing and inspect the page I get :

General

Form

Thanks for your help !


Solution

  • The problem in this particular example is the start date ('st_date'='04/13/2021') being a date after the end date ('end_date'='04/13/2002'). The request is hence basically for data from after 2021 but before 2002, which doesn't quite make sense.

    Changing the latter to any date after 04/13/2021 in this scenario should solve the problem.