python - Trying to web scrape S&P500 data from Yahoo Finance but cannot retrieve despite all right format - Stack Overfl

I have been attempting to web scrape data from yahoo finance specifically on historical data of S&P

I have been attempting to web scrape data from yahoo finance specifically on historical data of S&P 500 which its webpage url is '/%5EGSPC/history/?period1=1574074965&period2=1731927744'

As you can see from the picture below, it seems that yahoo finance is not providing access to my act of web scraping. Is there any other solution to overcome this and web scrape the data

S&P 500 data

I have been attempting to web scrape data from yahoo finance specifically on historical data of S&P 500 which its webpage url is 'https://finance.yahoo/quote/%5EGSPC/history/?period1=1574074965&period2=1731927744'

As you can see from the picture below, it seems that yahoo finance is not providing access to my act of web scraping. Is there any other solution to overcome this and web scrape the data

S&P 500 data

Share Improve this question asked Nov 18, 2024 at 11:29 jumbojumbo 111 bronze badge 2
  • Can you confirm whether you want the Table of content available in the URL you provided? – Moses01 Commented Nov 18, 2024 at 11:59
  • Please make sure to post code and console output as code-fenced text, not images. Images can't be copy-pasted, load slower, take more bandwidth, and are less accessible. – Anerdw Commented Nov 19, 2024 at 0:21
Add a comment  | 

1 Answer 1

Reset to default 2

Since the URL you use is Yahoo Finance and it is redirecting to multiple sites and fetching the data, but the beautifulsoup you use can try fetch up to 30 redirects only.

Instead of using Web Scrap, You can use Yahoo Finance module for Python. I noticed, You wanted to fetch the data from Nov 18,2019 to Nov 18,2024

So please use this code below to get the required data. You can change the dates as per your wish or use the below line to get all data

data = sp500.history(period="max")

Here is the code you should use:

import yfinance as yf
ticker = "^GSPC"
data = yf.Ticker(ticker)
hist = data.history(start="2019-11-18", end="2024-11-18")  # Specify date range
hist.to_csv("sp500_data.csv")

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745621616a4636551.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信