Python Forum
download from python - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Networking (https://python-forum.io/forum-12.html)
+--- Thread: download from python (/thread-21480.html)



download from python - mpellegrini - Oct-01-2019

I'd like to download a file present at follow link

But the follow code python get me the error "HTTPError: Unauthorized":

import urllib.request
url = 'https://query1.finance.yahoo.com/v7/finance/download/TEN.MI?period1=1451602800&period2=1569708000&interval=1d&events=history&crumb=NZczOoN5ibT'
urllib.request.urlretrieve(url, 'C:/dati/xxx.csv')


RE: download from python - Larz60+ - Oct-01-2019

There's something wrong with your url.
If you try to run directly from browser, you get following:
Output:
finance error code "Unauthorized" description "Invalid cookie"



RE: download from python - mpellegrini - Oct-02-2019

(Oct-01-2019, 07:32 PM)Larz60+ Wrote: There's something wrong with your url.
If you try to run directly from browser, you get following:
Output:
finance error code "Unauthorized" description "Invalid cookie"

This is the error that I obtain from code, but from browser Chrome I obtain the downoload of a csv file.


RE: download from python - stranac - Oct-02-2019

Sounds like you're missing an authorization step then,and your browser has already been authorized.
You probably need to login to use that api.


RE: download from python - mpellegrini - Oct-03-2019

(Oct-02-2019, 07:54 AM)stranac Wrote: Sounds like you're missing an authorization step then,and your browser has already been authorized.
You probably need to login to use that api.

How Can I do to obtain what you say directly from code python?
I'm interested in automatically getting the file from the web

I set it as Chrome's default browser, but the script continues to give error "HTTPError: Unauthorized"


RE: download from python - stranac - Oct-03-2019

You write code that will log you in and then keep using a logged-in session.
You'll need to figure out what requests your browser makes to log you in and recreate those in your code.


RE: download from python - mpellegrini - Oct-03-2019

(Oct-03-2019, 10:34 AM)stranac Wrote: You write code that will log you in and then keep using a logged-in session.
You'll need to figure out what requests your browser makes to log you in and recreate those in your code.
quite right!
This code works:
import webbrowser
import urllib.request
url = 'https://query1.finance.yahoo.com/v7/finance/download/TEN.MI?period1=1451602800&period2=1569708000&interval=1d&events=history&crumb=NZczOoN5ibT'
chrome_path = 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s'
webbrowser.get(chrome_path).open(url)