investpy icon indicating copy to clipboard operation
investpy copied to clipboard

πŸ› Fix issues with the latest API used by Investing.com to pull data

Open alvarobartt opened this issue 1 year ago β€’ 33 comments

πŸ› Bug Fixes

  • Both get_stock_recent_data and get_stock_historical_data have been fixed and using https://api.investing.com/api/financialdata/historical instead of https://www.investing.com/instruments/HistoricalDataAjax

alvarobartt avatar Sep 16 '22 09:09 alvarobartt

@alvarobartt nice work with the fix! Any ETA on when this may be merged to master and updated in pip?

jmizgajski avatar Sep 16 '22 16:09 jmizgajski

I also still get the 403 error. Are you still on it @alvarobartt ? Do you think you can fix it? – Is it working with you, @jmizgajski ?

ymyke avatar Sep 17 '22 14:09 ymyke

@alvarobartt

When will you fix "get_currency_cross_historical_data"?

File "D:\Anaconda3\lib\site-packages\investpy\currency_crosses.py", line 674, in get_currency_cross_historical_data raise ConnectionError( ConnectionError: ERR#0015: error 403, try again later.

hscho77 avatar Sep 19 '22 00:09 hscho77

Hi everyone! So it was working but it lasted just some requests, then it got blocked, not due to any sort of new Investing.com protection against investpy, but actually due to Cloudflare, as they included protection in the latest release of Investing.com, so the previous API was deprecated and the current one is protected... I'll keep on exploring but it seems that there's nothing left to test 😞

alvarobartt avatar Sep 19 '22 06:09 alvarobartt

Thanks for trying, @alvarobartt !

ymyke avatar Sep 19 '22 06:09 ymyke

@alvarobartt For your information to make sure, the ajax api https://www.investing.com/instruments/HistoricalDataAjax still working only when user operate web browser. Cloudflare's WAF started to block direct http connection by script or automation.

mic-user avatar Sep 19 '22 07:09 mic-user

Hi everyone! So it was working but it lasted just some requests, then it got blocked, not due to any sort of new Investing.com protection against investpy, but actually due to Cloudflare, as they included protection in the latest release of Investing.com, so the previous API was deprecated and the current one is protected... I'll keep on exploring but it seems that there's nothing left to test 😞

Have you tested a sleep of 1 to 5 seconds between calls? I have been using that with investpy. Maybe it will not trigger the Cloudflare's WAF. If not possible some quotes are still better than none. Thanks for the work BTW

LuisSousaSilva avatar Sep 19 '22 13:09 LuisSousaSilva

So, will investpy become a dead project? I hope not so, since I wrote a lot of code depending on that beautiful library. Perhaps a solution can be found that is also slightly profitable for investing.com, so that they continue to maintain reasonable access to their data through known end-points. In any case, thanks a lot for the work done so far on this project.

vid1998 avatar Sep 20 '22 09:09 vid1998

Thank you for taking a look @alvarobartt - am using investpy for my own analysis and found it awesome! Hope there can be a way round...

RPDev2002 avatar Sep 20 '22 10:09 RPDev2002

I was using this to get data for my thesis, thankfully I had the whole data backed up but now I'll have to rewrite a lot of functions. Thanks for trying to solve this problem and for creating this amazing library in the first place. Hope this somehow gets solved.

Belbute avatar Sep 20 '22 13:09 Belbute

You have created something wonderful. It was soo useful for me to carryout various analysis using investing.com. ( investing.com was the only site provided the data I needed) . I am sincerely hoping that you will be able to find a way around this issue. Hoping for the best.

sampathkar avatar Sep 20 '22 15:09 sampathkar

@alvarobartt have you tried allowing requests through a paid proxy provider? this could be a workaround for those willing to pay a proxy provider. Many scraping libs do it.

Also if you need to simulate the browser maybe something like https://github.com/pyppeteer/pyppeteer could come in handy. This is a puppeteer clone but pure python, installable from pip.

jmizgajski avatar Sep 20 '22 20:09 jmizgajski

@alvarobartt have you tried allowing requests through a paid proxy provider? this could be a workaround for those willing to pay a proxy provider. Many scraping libs do it.

Also if you need to simulate the browser maybe something like https://github.com/pyppeteer/pyppeteer could come in handy. This is a puppeteer clone but pure python, installable from pip.

If this was an option I would be interested..

RPDev2002 avatar Sep 20 '22 22:09 RPDev2002

Interesting that this seems to have appeared on Investing Com 6 days ago about not having an API..

https://www.investing-support.com/hc/en-gb/articles/115005473825-Do-you-provide-an-API-

RPDev2002 avatar Sep 21 '22 12:09 RPDev2002

This guy is using Puppeteer – apparently with some success: https://github.com/DavideViolante/investing-com-api/issues/68#issuecomment-1253834929 Maybe worth a try, @alvarobartt ?

ymyke avatar Sep 21 '22 15:09 ymyke

Hi everyone! So it was working but it lasted just some requests, then it got blocked, not due to any sort of new Investing.com protection against investpy, but actually due to Cloudflare, as they included protection in the latest release of Investing.com, so the previous API was deprecated and the current one is protected... I'll keep on exploring but it seems that there's nothing left to test disappointed

I just want to thank you for your application. It really feels like Cloudflare will stop at nothing to ruin all the fun on the internet.

kshabahang avatar Sep 21 '22 18:09 kshabahang

This guy is using Puppeteer – apparently with some success: DavideViolante/investing-com-api#68 (comment) Maybe worth a try, @alvarobartt ?

sound like it works using https://github.com/pyppeteer/pyppeteer, need to change api call a bit, but works

GSLabIt avatar Sep 27 '22 15:09 GSLabIt

Yeah, it seems to work using Puppeteer, here the changes between the previous version if you need. Also remember to set a user agent or it keeps not working (Cloudflare makes a good job on blocking some traffic). Idk Python much so I can't help much more than this.

DavideViolante avatar Sep 27 '22 16:09 DavideViolante

Yeah, it seems to work using Puppeteer, here the changes between the previous version if you need. Also remember to set a user agent or it keeps not working (Cloudflare makes a good job on blocking some traffic). Idk Python much so I can't help much more than this.

Thanks, i used it to make it work in python, here is the code:


    AGENTS = (
        'Mozilla/5.0 (Windows NT 10.0; Win64; x64) '
        'AppleWebKit/537.36 (KHTML, like Gecko) '
        'Chrome/105.0.0.0 Safari/537.36'
    )

    URL = (
        'https://api.investing.com/api/financialdata/{id}'
        '/historical/chart?period={p}&interval={i}&pointscount=120'
    )

    async def _get_investing_com_data(self, symbol, period='P1M', interval='P1D'):
        url = URL.format(id=symbol, p=period, i=interval)
        browser = await launch(
            handleSIGINT=False, handleSIGTERM=False, handleSIGHUP=False
        )
        page = await browser.newPage()
        await page.setUserAgent(AGENTS)
        await page.goto(url)
        element = await page.querySelector('body')
        content = await page.evaluate('(element) => element.textContent', element)
        await browser.close()
        return json.loads(content).get('data', False)

Do you know how the endpoint to pass startDate and endDate?

GSLabIt avatar Sep 27 '22 16:09 GSLabIt


    AGENTS = (
        'Mozilla/5.0 (Windows NT 10.0; Win64; x64) '
        'AppleWebKit/537.36 (KHTML, like Gecko) '
        'Chrome/105.0.0.0 Safari/537.36'
    )

    URL = (
        'https://api.investing.com/api/financialdata/{id}'
        '/historical/chart?period={p}&interval={i}&pointscount=120'
    )

    async def _get_investing_com_data(self, symbol, period='P1M', interval='P1D'):
        url = URL.format(id=symbol, p=period, i=interval)
        browser = await launch(
            handleSIGINT=False, handleSIGTERM=False, handleSIGHUP=False
        )
        page = await browser.newPage()
        await page.setUserAgent(AGENTS)
        await page.goto(url)
        element = await page.querySelector('body')
        content = await page.evaluate('(element) => element.textContent', element)
        await browser.close()
        return json.loads(content).get('data', False)

This code fails for me and returns False. trying to return json.loads(content) alone gives the error: {'@errors': ['Core API respond with invalid status: 500']}

sahelanthropussy avatar Oct 07 '22 03:10 sahelanthropussy


    AGENTS = (
        'Mozilla/5.0 (Windows NT 10.0; Win64; x64) '
        'AppleWebKit/537.36 (KHTML, like Gecko) '
        'Chrome/105.0.0.0 Safari/537.36'
    )

    URL = (
        'https://api.investing.com/api/financialdata/{id}'
        '/historical/chart?period={p}&interval={i}&pointscount=120'
    )

    async def _get_investing_com_data(self, symbol, period='P1M', interval='P1D'):
        url = URL.format(id=symbol, p=period, i=interval)
        browser = await launch(
            handleSIGINT=False, handleSIGTERM=False, handleSIGHUP=False
        )
        page = await browser.newPage()
        await page.setUserAgent(AGENTS)
        await page.goto(url)
        element = await page.querySelector('body')
        content = await page.evaluate('(element) => element.textContent', element)
        await browser.close()
        return json.loads(content).get('data', False)

This code fails for me and returns False. trying to return json.loads(content) alone gives the error: {'@errors': ['Core API respond with invalid status: 500']}

Hi @sahelanthropussy, I've released investiny as a temporary replacement for investpy while I wait to get a response from Investing.com, so use it in the meantime instead! Thanks πŸ€—

P.S. If you have any doubts don't hesitate asking me, and open all the related feature requests and bug reports at https://github.com/alvarobartt/investiny/issues or open a discussion at https;//github.com/alvarobartt/investiny/discussions

alvarobartt avatar Oct 07 '22 09:10 alvarobartt

Hi @GSLabIt please check the comment above regarding the release of investiny, thanks πŸ€—

alvarobartt avatar Oct 07 '22 09:10 alvarobartt

Ah wow thank you alvarobartt... will try it out soon..

RPDev2002 avatar Oct 07 '22 09:10 RPDev2002

Hi @GSLabIt please check the comment above regarding the release of investiny, thanks πŸ€—

Hi will have a look. Any chance to have it compatible with python 3.8?

GSLabIt avatar Oct 07 '22 09:10 GSLabIt

Hi @GSLabIt please check the comment above regarding the release of investiny, thanks πŸ€—

Hi will have a look. Any chance to have it compatible with python 3.8?

Sure! Feel free to open an issue at https://github.com/alvarobartt/investiny/issues and I'll try to add it ASAP πŸ‘πŸ»

alvarobartt avatar Oct 07 '22 10:10 alvarobartt

Ah wow thank you alvarobartt... will try it out soon..

Cool @RPDev2002, thank you too!

alvarobartt avatar Oct 07 '22 10:10 alvarobartt

Good morning @alvarobartt, thank you so much for the investpy code it is a really helpful tool. I has been used the following Investing.com url for a personal finance project inspired by your code:

url = "https://www.investing.com/instruments/HistoricalDataAjax"

I have tried to change my code to the:

url = f"https://api.investing.com/api/financialdata/historical/{id_}"

But I keep getting the ERR:403 as you have mentioned in the different posts. Have you achieved to make it work with the new investing.com API?

Thank you so much in advance.

PD: In case you havenΒ΄t figure out yet I will start trying with the code from investiny

alesegura96 avatar Oct 18 '22 13:10 alesegura96

Hi @alesegura96, as you point out, both Investing.com APIs https://www.investing.com/instruments/HistoricalDataAjax and https://api.investing.com/api/financialdata/historical are not working fine as those block all the incoming requests with HTTP 403... So I'd suggest you and everyone to use investiny in the meantime, as it's currently stable (just fixed a critical bug) and performs well.

Let's see if I can get to contact Investing.com, and get a response from them so as to continue the development of investpy either as a personal project or as an Investing.com project...

alvarobartt avatar Oct 18 '22 14:10 alvarobartt

Did you try using Puppeteer as we mentioned before? @alvarobartt I think it's the smallest change to make it work, at least it was for my project

DavideViolante avatar Oct 20 '22 06:10 DavideViolante