Home > Net >  Python: request to website doesn't work in any cases
Python: request to website doesn't work in any cases

Time:11-26

I want to write a function which collects data from yahoo finance site. The website request looks like that:

import requests
def yahoo_summary_stats(stock):
    response = requests.get(f"https://finance.yahoo.com/quote/{stock}")
    print(response.reason)

if I call the function with parameter 'ALB':

yahoo_summary_stats('ALB')

everything works fine and the request is ok. He correctly leads me to: https://finance.yahoo.com/quote/ALB

The call:

yahoo_summary_stats('AEE')

on other hand should lead me to the site https://finance.yahoo.com/quote/AEE, which I can call without any problems in firefox.

The program for some reason gives me a 'Not found' error. What is the problem of my request to that website?

CodePudding user response:

Try to set User-Agent in headers...

def yahoo_summary_stats(stock):
    response = requests.get(f"http://finance.yahoo.com/quote/{stock}", headers={'User-Agent': 'Custom user agent'})
    print(response.status_code)
    print(response.reason)

yahoo_summary_stats('ALB')
yahoo_summary_stats('AEE')
  • Related