urllib.error.HTTPError: HTTP Error 404: Not Found Issue when Trying to read url from Yahoo Finance given Stock Symbol

274 Views Asked by At

I am trying to write a program that will help me with accounting work. I am trying to pull information by using stock prices for companies over time. I am utilizing urllib.request to read and open URLs, but I can't get around this 404 issue, despite the URL existing. Any help would be appreciated.

I have tried changing the import statement to:

import urllib.request as ur

so that I can also try:

read_data = ur.urlopen(url_is).read() 
soup_is = BeautifulSoup(read_data, 'lxml')

but that came up with the same error.

1

There are 1 best solutions below

0
Denis Skopa On

To extract historical market data in 4 lines you can use yfinance package.

Check code in the online IDE.

import yfinance as yf

mar = yf.Ticker("MAR")

# get historical market data
hist = mar.history(period="1mo")

print(hist)

Output:

                                 Open  ...  Stock Splits
Date                                   ...              
2023-02-08 00:00:00-05:00  173.312661  ...           0.0
2023-02-09 00:00:00-05:00  175.148339  ...           0.0
2023-02-10 00:00:00-05:00  172.734024  ...           0.0
2023-02-13 00:00:00-05:00  171.626626  ...           0.0
2023-02-14 00:00:00-05:00  175.307981  ...           0.0
2023-02-15 00:00:00-05:00  180.495778  ...           0.0
2023-02-16 00:00:00-05:00  176.984033  ...           0.0
2023-02-17 00:00:00-05:00  174.220532  ...           0.0
2023-02-21 00:00:00-05:00  170.359599  ...           0.0
2023-02-22 00:00:00-05:00  169.641289  ...           0.0
2023-02-23 00:00:00-05:00  171.529999  ...           0.0
2023-02-24 00:00:00-05:00  169.470001  ...           0.0
2023-02-27 00:00:00-05:00  171.759995  ...           0.0
2023-02-28 00:00:00-05:00  169.559998  ...           0.0
2023-03-01 00:00:00-05:00  169.419998  ...           0.0
2023-03-02 00:00:00-05:00  169.389999  ...           0.0
2023-03-03 00:00:00-05:00  171.850006  ...           0.0
2023-03-06 00:00:00-05:00  173.289993  ...           0.0
2023-03-07 00:00:00-05:00  175.580002  ...           0.0

[19 rows x 7 columns]

Also in your case, the problem may be due to the fact that your request is blocked.

You can read Reducing the chance of being blocked while web scraping blog post to figure out how to bypass these blocks.