I am trying to retrieve the list of available option expiries for a given ticker on yahoo finance. For instance using SPY as ticker on https://finance.yahoo.com/quote/SPY/options
The list of expiries are in the drop down list:
<div class="Fl(start) Pend(18px) option-contract-control drop-down-selector" data-reactid="4">
<select class="Fz(s)" data-reactid="5">
<option selected="" value="1576627200" data-reactid="6">December 18, 2019</option>
<option value="1576800000" data-reactid="7">December 20, 2019</option>
<option value="1577059200" data-reactid="8">December 23, 2019</option>
...
< / select >
< / div >
Using the div class name (or the select class name, but there seems to be several of these on the page), I get the list of values as a single string of concatenated expiries.
My function (I pass on ticker='SPY' from the main function):
def get_list_expiries(ticker):
browser = webdriver.Chrome()
options_url = "https://finance.yahoo.com/quote/" + str(ticker) + "/options"
browser.get(options_url)
html_source = browser.page_source
soup = BeautifulSoup(html_source, 'html.parser')
expiries_dt = []
for exp in soup.find_all(class_="Fl(start) Pend(18px) option-contract-control drop-down-selector"):
expiries_dt.append(exp.text)
browser.quit()
return expiries_dt
This produces:
['December 18, 2019December 20, 2019December 23, 2019December 24, 2019December 27, 2019December 30, 2019...']
I understand I need to use selenium for this but I can't figure out how. The result is always a list of a single string. Ideally I would like to return two lists: one with the unix datestamp (option value="1576627200") and another list with the 'normal' dates (ie 18/12/2019).
Any help will be greatly appreciated.