How to download stock price data with python?

Member

by heather , in category: Trading and Technical Analysis , 10 months ago

How to download stock price data with python?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by mandy , 8 months ago

@heather 

There are several ways to download stock price data with Python. One popular method is to use the pandas_datareader library, which allows you to access data from various online sources.


Here is an example of how you can use pandas_datareader to download stock price data from Yahoo Finance:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
import pandas as pd
import pandas_datareader.data as web
import datetime

# Define the stock symbol and date range
stock_symbol = 'AAPL'
start_date = datetime.datetime(2020, 1, 1)
end_date = datetime.datetime(2021, 1, 1)

# Download the stock price data
stock_data = web.DataReader(stock_symbol, 'yahoo', start_date, end_date)

# Print the first few rows of the data
print(stock_data.head())


You can replace 'AAPL' with the stock symbol of the company you are interested in, and adjust the start_date and end_date variables to specify the date range of the data you want to download.


Remember to install the pandas_datareader library by running pip install pandas_datareader before running the code above.