@@ -661,31 +661,35 @@ def get_options_data(self, month=None, year=None, expiry=None):
661
661
662
662
_OPTIONS_BASE_URL = 'http://finance.yahoo.com/q/op?s={sym}'
663
663
664
- def _get_option_tables (self , month , year , expiry ):
664
+ def _get_option_tables (self , expiry ):
665
+ root = self ._get_option_page_from_yahoo (expiry )
666
+ tables = self ._parse_option_page_from_yahoo (root )
667
+ m1 = _two_char_month (expiry .month )
668
+ table_name = '_tables' + m1 + str (expiry .year )[- 2 :]
669
+ setattr (self , table_name , tables )
670
+ return tables
665
671
666
- year , month , expiry = self . _try_parse_dates ( year , month , expiry )
672
+ def _get_option_page_from_yahoo ( self , expiry ):
667
673
668
674
url = self ._OPTIONS_BASE_URL .format (sym = self .symbol )
669
675
670
- if month and year : # try to get specified month from yahoo finance
671
- m1 = _two_char_month (month )
676
+ m1 = _two_char_month (expiry .month )
672
677
673
- # if this month use other url
674
- if month == CUR_MONTH and year == CUR_YEAR :
675
- url += '+Options'
676
- else :
677
- url += '&m={year}-{m1}' .format (year = year , m1 = m1 )
678
- else : # Default to current month
678
+ # if this month use other url
679
+ if expiry .month == CUR_MONTH and expiry .year == CUR_YEAR :
679
680
url += '+Options'
681
+ else :
682
+ url += '&m={year}-{m1}' .format (year = expiry .year , m1 = m1 )
680
683
681
684
root = self ._parse_url (url )
685
+ return root
686
+
687
+ def _parse_option_page_from_yahoo (self , root ):
688
+
682
689
tables = root .xpath ('.//table' )
683
690
ntables = len (tables )
684
691
if ntables == 0 :
685
- raise RemoteDataError ("No tables found at {0!r}" .format (url ))
686
-
687
- table_name = '_tables' + m1 + str (year )[- 2 :]
688
- setattr (self , table_name , tables )
692
+ raise RemoteDataError ("No tables found" )
689
693
690
694
try :
691
695
self .underlying_price , self .quote_time = self ._get_underlying_price (root )
@@ -723,7 +727,7 @@ def _get_option_data(self, month, year, expiry, name):
723
727
try :
724
728
tables = getattr (self , table_name )
725
729
except AttributeError :
726
- tables = self ._get_option_tables (month , year , expiry )
730
+ tables = self ._get_option_tables (expiry )
727
731
728
732
ntables = len (tables )
729
733
table_loc = self ._TABLE_LOC [name ]
@@ -948,6 +952,8 @@ def _try_parse_dates(year, month, expiry):
948
952
year = CUR_YEAR
949
953
month = CUR_MONTH
950
954
expiry = dt .date (year , month , 1 )
955
+ else :
956
+ expiry = dt .date (year , month , 1 )
951
957
952
958
return year , month , expiry
953
959
@@ -1127,7 +1133,11 @@ def _get_expiry_months(self):
1127
1133
url = 'http://finance.yahoo.com/q/op?s={sym}' .format (sym = self .symbol )
1128
1134
root = self ._parse_url (url )
1129
1135
1130
- links = root .xpath ('.//*[@id="yfncsumtab"]' )[0 ].xpath ('.//a' )
1136
+ try :
1137
+ links = root .xpath ('.//*[@id="yfncsumtab"]' )[0 ].xpath ('.//a' )
1138
+ except IndexError :
1139
+ return RemoteDataError ('Expiry months not available' )
1140
+
1131
1141
month_gen = (element .attrib ['href' ].split ('=' )[- 1 ]
1132
1142
for element in links
1133
1143
if '/q/op?s=' in element .attrib ['href' ]
0 commit comments