benjamincham / freeoptionschain

Free Options Chain
MIT License
25 stars 3 forks source link

NameError: name 'HTMLSession' is not defined #16

Open danielhaviv opened 1 month ago

danielhaviv commented 1 month ago

Trying to run through the example in the Readme raises:

(options) daniel.haviv@W6NGC7J67V options % python test.py Traceback (most recent call last): File "/Users/daniel.haviv/Documents/Projects/options/test.py", line 6, in options_chain = ref_FOC.get_expiration_dates("AAPL") ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/daniel.haviv/Documents/Projects/options/lib/python3.12/site-packages/FOC/main.py", line 43, in get_expiration_dates expiration_dates = op.get_expiration_dates(ticker) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/daniel.haviv/Documents/Projects/options/lib/python3.12/site-packages/yahoo_fin/options.py", line 101, in get_expiration_dates session = HTMLSession() ^^^^^^^^^^^ NameError: name 'HTMLSession' is not defined

Steps to reproduce:

  1. python3 -m venv options
  2. pip install freeoptionschain
  3. run the following code:
    
    #create instance
    ref_FOC = FOC()

Fetch options expiration dates for 'AAPL'

options_chain = ref_FOC.get_expiration_dates("AAPL")`

danielhaviv commented 1 month ago

removing the exception block from the imports (so just importing HTMLSession) raises the following exception:

Traceback (most recent call last): File "/Users/daniel.haviv/Documents/Projects/options/test.py", line 1, in from FOC import FOC File "/Users/daniel.haviv/Documents/Projects/options/lib/python3.12/site-packages/FOC/init.py", line 5, in from .main import FOC File "/Users/daniel.haviv/Documents/Projects/options/lib/python3.12/site-packages/FOC/main.py", line 8, in from yahoo_fin import options as op File "/Users/daniel.haviv/Documents/Projects/options/lib/python3.12/site-packages/yahoo_fin/options.py", line 7, in from requests_html import HTMLSession File "/Users/daniel.haviv/Documents/Projects/options/lib/python3.12/site-packages/requests_html.py", line 14, in from lxml.html.clean import Cleaner File "/Users/daniel.haviv/Documents/Projects/options/lib/python3.12/site-packages/lxml/html/clean.py", line 18, in raise ImportError( ImportError: lxml.html.clean module is now a separate project lxml_html_clean. Install lxml[html_clean] or lxml_html_clean directly.

danielhaviv commented 1 month ago

Installing lxml_html_clean raises the following exception:

File "/Users/daniel.haviv/Documents/Projects/options/test.py", line 6, in options_chain = ref_FOC.get_expiration_dates("AAPL") ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/daniel.haviv/Documents/Projects/options/lib/python3.12/site-packages/FOC/main.py", line 45, in get_expiration_dates expiration_dates[idx] = datetime.strptime(expiration_date, "%B %d, %Y").strftime("%Y-%m-%d") ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/opt/homebrew/Cellar/python@3.12/3.12.2_1/Frameworks/Python.framework/Versions/3.12/lib/python3.12/_strptime.py", line 554, in _strptime_datetime tt, fraction, gmtoff_fraction = _strptime(data_string, format) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/opt/homebrew/Cellar/python@3.12/3.12.2_1/Frameworks/Python.framework/Versions/3.12/lib/python3.12/_strptime.py", line 333, in _strptime raise ValueError("time data %r does not match format %r" % ValueError: time data '\n' does not match format '%B %d, %Y'

tddschn commented 3 weeks ago

Got the Same error

powellnorma commented 2 days ago

ValueError: time data '\n' does not match format '%B %d, %Y'

First step to fix this is using updated yahoo_fin:

pip install yahoo_fin_cheese https://github.com/atreadw1492/yahoo_fin/pull/121

2.

--- a/FOC/main.py
+++ b/FOC/main.py

     def get_expiration_dates(self,ticker):
         expiration_dates = op.get_expiration_dates(ticker)
         for idx, expiration_date in enumerate(expiration_dates):
-            expiration_dates[idx] = datetime.strptime(expiration_date, "%B %d, %Y").strftime("%Y-%m-%d")
+            expiration_dates[idx] = datetime.strptime(expiration_date, "%b %d, %Y").strftime("%Y-%m-%d")
         return expiration_dates

But now I get:

[..]
  File "/freeoptionschain/FOC/main.py", line 119, in json_extract_node
    raise ValueError("Invalid json keys."

The response from the nasdaq API is:

{'data': None, 'message': None, 'status': {'rCode': 400, 'bCodeMessage': [{'code': 1001, 'errorMessage': 'Symbol not exists.'}], 'developerMessage': None}}