-1

I am having trouble accessin Yahoo Finance. I am getting the 401 access error. Can you help?

from urllib import request

biib_url = 'https://query1.finance.yahoo.com/v7/finance/download/BIIB?period1=1463674892&period2=1495210892&interval=1d&events=history&crumb=DHH18j0z8Nl'

def download_stock_data(csv_url):
    response = request.urlopen(csv_url)
    csv = response.read()
    csv_str = str(csv)
    lines = csv_str.split("\\n")
    dest_url = r'biib.csv'
    fx = open(dest_url, "w")
    for line in lines:
    fx.write(line + "\n")
    fx.close()

download_stock_data(biib_url)   

1 Answers1

3

Change your code to this.

#~ from urllib import request
import requests

biib_url = 'https://query1.finance.yahoo.com/v7/finance/download/BIIB?period1=1463674892&period2=1495210892&interval=1d&events=history&crumb=DHH18j0z8Nl'

def download_stock_data(csv_url):
    #~ response = request.urlopen(csv_url)
    #~ csv = response.read()
    #~ csv_str = str(csv)
    csv_str = requests.get(csv_url).text
    lines = csv_str.split("\\n")
    dest_url = r'biib.csv'
    fx = open(dest_url, "w")
    for line in lines:
        fx.write(line + "\n")
    fx.close()

download_stock_data(biib_url)   

In other words, use requests instead of the (older) urllib. Your life will be a lot easier.

Bill Bell
  • 21,021
  • 5
  • 43
  • 58
  • i want to do a website that gets financial data from an api and returns some calculations for the end user. user inserts question, website returns the answer and displays some graphs The data that the site uses to come up with the answer, comes from doing API calls to a paid site. Where do i do the calculations. Do i use MongoDB. What is your suggestion? thanks, Diogo – Diogo Marques May 20 '17 at 19:21
  • I'm not an expert about websites for doing that type of task, and SO doesn't welcome questions like the one you're asking me. I would suggest asking on https://softwarerecs.stackexchange.com/ – Bill Bell May 20 '17 at 22:45