Python Forum

Full Version: download from python
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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')
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"
(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.
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.
(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"
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.
(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)