'Include date range on Yahoo Financial scraper in Python

I found this wonderful tool made in Python to scrape data from Yahoo Finance website: https://github.com/JECSand/yahoofinancials

How do I pass a date range to this and include it into the script?

I would like to be able to see data on specific date range, by entering command like this:

python3 my_python_scraper.py TWTR, '2022-02-16', '2022-02-16'

Or something similar.

I can see there is a function get_historical_price_data(start_date, end_date, time_interval) but how do I pass dates to this script?

Any help is welcome.

Thank you



Solution 1:[1]

Here you go.

from utils import *
import time
import numpy as np
import pandas as pd
import datetime
import seaborn as sns
import matplotlib.pyplot as plt
import math
import warnings
warnings.filterwarnings("ignore")



def parser(x):
    return datetime.datetime.strptime(x,'%Y-%m-%d')


import yfinance as yf

# Get the data for the stock AAPL
start = '2020-01-01'
end = '2022-01-13'

data = yf.download('TWTR', start, end)


data = data.reset_index()
data

data.dtypes


# re-name field from 'Adj Close' to 'Adj_Close'
data = data.rename(columns={"Adj Close": "Adj_Close"})
data


data = data.loc[:,['Date','Adj_Close']]
       


plt.figure(figsize=(14, 5), dpi=100)
plt.plot(data['Date'], data['Adj_Close'], label='Twitter Stock Price')
plt.xlabel('Date')
plt.ylabel('USD')
plt.title('Figure 2: Twitter Stock Price')
plt.legend()
plt.show()

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ASH