1

I'm new to pyhton and just tried to write data from external file. I have no idea where i'm going wrong. can anyone please help me with this. Thanks in advance.

from urllib import request

url = r'https://query1.finance.yahoo.com/v7/finance/download/AMD?period1=1497317134&period2=1499909134&interval=1d&events=history&crumb=HwDtuBHqtg0'

def download_csv(csv_url):

    csv = request.urlopen(csv_url)
    csv_data = csv.read
    csv_str = str(csv_data)
    file = csv_str.split('\\n')
    dest_url = r'appl.csv'
    wr = open(dest_url, 'w')
    for data in file:
        wr.write(data + '\n')
    wr.close()

download_csv(url)
Andrea Corbellini
  • 17,339
  • 3
  • 53
  • 69
Krish
  • 25
  • 1
  • 1
  • 6

1 Answers1

0

So I ran the URL in the browser, and it states clearly that your API requires a cookie.

So you must provide a proper header, usually with urllib you can manage sessions but honestly I would opt for a more user-friendly library, such as the requests python library (HTTP for Humans)

Example:

s = requests.Session()

s.get('http://httpbin.org/cookies/set/sessioncookie/123456789')
r = s.get('http://httpbin.org/cookies')

print(r.text)
# '{"cookies": {"sessioncookie": "123456789"}}'

More: http://docs.python-requests.org/en/master/user/advanced/#session-objects

Fabien
  • 4,862
  • 2
  • 19
  • 33