oscar0812 / pyfinviz

A python package to scrape data from finviz.com
Other
40 stars 9 forks source link

`Quote.__get_outer_ratings_df__` fails when `o_tds_text` is an empty list. #8

Closed ivnle closed 1 year ago

ivnle commented 1 year ago

Adding the check below resolves the issue.

    @staticmethod
    def __get_outer_ratings_df__(soup):
        outer_ratings_table = soup.find('table', class_='fullview-ratings-outer')
        # might not have outer ratings
        if outer_ratings_table is None:
            return None

        outer_ratings_trs = outer_ratings_table.find_all('tr', recursive=False)

        outer_ratings_info = []
        tags__ = ['Date', 'Status', 'Outer', 'Rating', 'Price']
        for tr in outer_ratings_trs:
            o_tds_text = [td.text for td in tr.find_all('td')[1:]]
            if len(o_tds_text) == len(tags__): <<<<<<<<<<<<<<<<<<<<<<<
                outer_ratings_info.append({tags__[i]: o_tds_text[i] for i in range(0, len(tags__))})

        return pd.DataFrame(outer_ratings_info)
oscar0812 commented 1 year ago

Hello. When is this happening? What function are you calling and with which arguments? A stack trace is helpful.

drampelt commented 1 year ago

I'm getting an error when fetching a Quote, I'm guessing it's the same problem. Here's the code and stack trace:

>>> q = Quote('AAPL')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/.../lib/python3.10/site-packages/pyfinviz/quote.py", line 130, in __init__
    self.outer_ratings_df = Quote.__get_outer_ratings_df__(self.soup)
  File "/.../lib/python3.10/site-packages/pyfinviz/quote.py", line 21, in __get_outer_ratings_df__
    outer_ratings_info.append({tags__[i]: o_tds_text[i] for i in range(0, len(tags__))})
  File "/.../lib/python3.10/site-packages/pyfinviz/quote.py", line 21, in <dictcomp>
    outer_ratings_info.append({tags__[i]: o_tds_text[i] for i in range(0, len(tags__))})
IndexError: list index out of range
ivnle commented 1 year ago

It is the same issue as @drampelt described.

oscar0812 commented 1 year ago

Thank you for the stack trace @drampelt. Looking into it.

oscar0812 commented 1 year ago

Issue is fixed. Please update to the latest version and confirm: pip install pyfinviz==0.10 I will close this issue once I receive confirmation. Thanks!

ivnle commented 1 year ago

Confirmed that 0.10 fixes the issue.