mariostoev / finviz

Unofficial API for finviz.com
MIT License
1.05k stars 276 forks source link

list index out of range #199

Open TZimm55 opened 10 months ago

TZimm55 commented 10 months ago

As of today 2023-10-13, I tried to run:

import finviz

print(finviz.get_stock('aapl'))

Error received:

File: "...\finviz\main_func.py", line 35, in get_stock title = page_parsed.cssselect('table[class="fullview-title"]')[0] IndexError: list index out of range

Additional Info:

sw8989 commented 10 months ago

+1, experienced the same issue

sw8989 commented 10 months ago

Just now

thirumalairajr commented 10 months ago

Fixed it. Can someone review this PR https://github.com/mariostoev/finviz/pull/200 ?

In the meantime, you can use this fork https://github.com/thirumalairajr/finviz @TZimm55 @sw8989

TZimm55 commented 10 months ago

Fixed it. Can someone review this PR #200 ?

In the meantime, you can use this fork https://github.com/thirumalairajr/finviz @TZimm55 @sw8989

Thank you thank you. I appreciate the promptness. I will try to figure out how to use a fork.

thirumalairajr commented 10 months ago

Fixed it. Can someone review this PR #200 ? In the meantime, you can use this fork https://github.com/thirumalairajr/finviz @TZimm55 @sw8989

Thank you thank you. I appreciate the promptness. I will try to figure out how to use a fork.

You can uninstall your local finviz dependency and install it from the fork

pip install -U git+https://github.com/thirumalairajr/finviz

Once the PR is merged, you can switch back to the main repo.

TZimm55 commented 10 months ago

Fixed it. Can someone review this PR #200 ? In the meantime, you can use this fork https://github.com/thirumalairajr/finviz @TZimm55 @sw8989

Thank you thank you. I appreciate the promptness. I will try to figure out how to use a fork.

You can uninstall your local finviz dependency and install it from the fork

pip install -U git+https://github.com/thirumalairajr/finviz

Once the PR is merged, you can switch back to the main repo.

Ahh, thank you very much, that worked! : ) That is what I needed.

sw8989 commented 10 months ago

@thirumalairajr unfortunately a new related error popped up.

stock_data = get_stock(ticker)
             ^^^^^^^^^^^^^^^^^

File "/opt/homebrew/lib/python3.11/site-packages/finviz/main_func.py", line 37, in get_stock data["Ticker"] = title.cssselect('h1[class="quote-header_ticker-wrapper_ticker"]')[0].text_content().strip()


IndexError: list index out of range

Any idea what is the issue this time ? 
ahloytan commented 10 months ago

@thirumalairajr hi, seems like finviz made another small update or something.

Line 37 Before: data["Ticker"] = title.cssselect('h1[class="quote-header_ticker-wrapper_ticker"]')[0].text_content().strip() After: data["Ticker"] = title.cssselect('h1[class="js-recent-quote-ticker quote-header_ticker-wrapper_ticker"]')[0].text_content().strip()

I have made the following change and it seems to work for me!

@sw8989 Not sure if you're able to edit the "main_func.py" as such, else I think we can wait for @thirumalairajr to make the change. Let me know if it helps/works

MahanyaKochhar commented 10 months ago

thanks @ahloytan fixed the issue based on your solution ! In the meantime, feel free to install from this fork https://github.com/MahanyaKochhar/finviz Steps -> Just uninstall your local finviz dependency and install it from the fork

pip install -U git+https://github.com/MahanyaKochhar/finviz

AdamMuzz commented 10 months ago

still experiencing this issue. @MahanyaKochhar Im not sure entirely why but using your fork messed up some numpy math in another area of my project. Specifically when I have your fork installed, a matrix multiplication results in all NaNs instead of the expected values.

eliluong commented 8 months ago

I am getting error on example code, too.

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
Cell In[5], line 2
      1 filters = ['exch_nasd', 'idx_sp500']
----> 2 stock_list = Screener(filters=filters, table='Performance', order='price')

File ~/notebooks/algotrading/algotrading/lib/python3.10/site-packages/finviz/screener.py:128, in Screener.__init__(self, tickers, filters, rows, order, signal, table, custom, user_agent, request_method)
    125 self._request_method = request_method
    127 self.analysis = []
--> 128 self.data = self.__search_screener()

File ~/notebooks/algotrading/algotrading/lib/python3.10/site-packages/finviz/screener.py:436, in Screener.__search_screener(self)
    421 """ Private function used to return data from the FinViz screener. """
    423 self._page_content, self._url = http_request_get(
    424     "https://finviz.com/screener.ashx",
    425     payload={
   (...)
    433     user_agent=self._user_agent,
    434 )
--> 436 self._rows = self.__check_rows()
    437 self.headers = self.__get_table_headers()
    439 if self._request_method == "async":

File ~/notebooks/algotrading/algotrading/lib/python3.10/site-packages/finviz/screener.py:404, in Screener.__check_rows(self)
    398 def __check_rows(self):
    399     """
    400     Checks if the user input for row number is correct.
    401     Otherwise, modifies the number or raises NoResults error.
    402     """
--> 404     self._total_rows = scrape.get_total_rows(self._page_content)
    406     if self._total_rows == 0:
    407         raise NoResults(self._url.split("?")[1])

File ~/notebooks/algotrading/algotrading/lib/python3.10/site-packages/finviz/helper_functions/scraper_functions.py:46, in get_total_rows(page_content)
     42 """ Returns the total number of rows(results). """
     44 total_element = page_content.cssselect('td[width="128"]')
     45 total_number = (
---> 46     etree.tostring(total_element[0]).decode("utf-8").split("</b>")[1].split()[0]
     47 )
     49 try:
     50     return int(total_number)

IndexError: list index out of range
asraghu123 commented 5 months ago

I am also facing the same issue

C:\Users\asraghu\vcp_screener.github.io-main>python -m vcp_screener Traceback (most recent call last): File "C:\Users\asraghu\AppData\Local\Programs\Python\Python39\lib\runpy.py", line 197, in _run_module_as_main return _run_code(code, main_globals, None, File "C:\Users\asraghu\AppData\Local\Programs\Python\Python39\lib\runpy.py", line 87, in _run_code exec(code, run_globals) File "C:\Users\asraghu\vcp_screener.github.io-main\vcp_screener.py", line 247, in stock_list = Screener(filters = filters, table = 'Performance' , order = 'asc') File "C:\Users\asraghu\AppData\Local\Programs\Python\Python39\lib\site-packages\finviz\screener.py", line 128, in init self.data = self.search_screener() File "C:\Users\asraghu\AppData\Local\Programs\Python\Python39\lib\site-packages\finviz\screener.py", line 436, in search_screener self._rows = self.check_rows() File "C:\Users\asraghu\AppData\Local\Programs\Python\Python39\lib\site-packages\finviz\screener.py", line 404, in check_rows self._total_rows = scrape.get_total_rows(self._page_content) File "C:\Users\asraghu\AppData\Local\Programs\Python\Python39\lib\site-packages\finviz\helper_functions\scraper_functions.py", line 46, in get_total_rows etree.tostring(total_element[0]).decode("utf-8").split("")[1].split()[0] IndexError: list index out of range

eusden commented 5 months ago

Ok here's what I did to get it working. Thank you @RiverRo

pip uninstall finviz
pip install git+https://github.com/RiverRo/finviz.git@main
DEPRECATION: Configuring installation scheme with distutils config files is deprecated and will no longer work in the near future. If you are using a Homebrew or Linuxbrew Python, please see discussion at https://github.com/Homebrew/homebrew-core/issues/76621
Collecting git+https://github.com/RiverRo/finviz.git@main
  Cloning https://github.com/RiverRo/finviz.git (to revision main) to /private/var/folders/hx/r87bbj7d3zv14zqdwqw3vgz00000gn/T/pip-req-build-3som4sf7
  Running command git clone --filter=blob:none --quiet https://github.com/RiverRo/finviz.git /private/var/folders/hx/r87bbj7d3zv14zqdwqw3vgz00000gn/T/pip-req-build-3som4sf7
  Running command git checkout -b main --track origin/main
  Switched to a new branch 'main'
  branch 'main' set up to track 'origin/main'.
  Resolved https://github.com/RiverRo/finviz.git to commit 2a2f8138ccd2dd17b5299e51f7808ecd8fcddf92
  Preparing metadata (setup.py) ... done
Requirement already satisfied: lxml in /opt/homebrew/lib/python3.9/site-packages (from finviz==1.4.6) (5.1.0)
Requirement already satisfied: requests in /opt/homebrew/lib/python3.9/site-packages (from finviz==1.4.6) (2.31.0)
Requirement already satisfied: aiohttp in /opt/homebrew/lib/python3.9/site-packages (from finviz==1.4.6) (3.9.3)
Requirement already satisfied: urllib3 in /opt/homebrew/lib/python3.9/site-packages (from finviz==1.4.6) (2.2.1)
Requirement already satisfied: cssselect in /opt/homebrew/lib/python3.9/site-packages (from finviz==1.4.6) (1.2.0)
Requirement already satisfied: user_agent in /opt/homebrew/lib/python3.9/site-packages (from finviz==1.4.6) (0.1.10)
Requirement already satisfied: beautifulsoup4 in /opt/homebrew/lib/python3.9/site-packages (from finviz==1.4.6) (4.12.3)
Requirement already satisfied: tqdm in /opt/homebrew/lib/python3.9/site-packages (from finviz==1.4.6) (4.66.2)
Requirement already satisfied: tenacity in /opt/homebrew/lib/python3.9/site-packages (from finviz==1.4.6) (8.2.3)
Requirement already satisfied: aiosignal>=1.1.2 in /opt/homebrew/lib/python3.9/site-packages (from aiohttp->finviz==1.4.6) (1.3.1)
Requirement already satisfied: attrs>=17.3.0 in /opt/homebrew/lib/python3.9/site-packages (from aiohttp->finviz==1.4.6) (23.2.0)
Requirement already satisfied: frozenlist>=1.1.1 in /opt/homebrew/lib/python3.9/site-packages (from aiohttp->finviz==1.4.6) (1.4.1)
Requirement already satisfied: multidict<7.0,>=4.5 in /opt/homebrew/lib/python3.9/site-packages (from aiohttp->finviz==1.4.6) (6.0.5)
Requirement already satisfied: yarl<2.0,>=1.0 in /opt/homebrew/lib/python3.9/site-packages (from aiohttp->finviz==1.4.6) (1.9.4)
Requirement already satisfied: async-timeout<5.0,>=4.0 in /opt/homebrew/lib/python3.9/site-packages (from aiohttp->finviz==1.4.6) (4.0.3)
Requirement already satisfied: soupsieve>1.2 in /opt/homebrew/lib/python3.9/site-packages (from beautifulsoup4->finviz==1.4.6) (2.5)
Requirement already satisfied: charset-normalizer<4,>=2 in /opt/homebrew/lib/python3.9/site-packages (from requests->finviz==1.4.6) (3.3.2)
Requirement already satisfied: idna<4,>=2.5 in /opt/homebrew/lib/python3.9/site-packages (from requests->finviz==1.4.6) (3.6)
Requirement already satisfied: certifi>=2017.4.17 in /opt/homebrew/lib/python3.9/site-packages (from requests->finviz==1.4.6) (2024.2.2)
Requirement already satisfied: six in /opt/homebrew/lib/python3.9/site-packages (from user_agent->finviz==1.4.6) (1.16.0)
Building wheels for collected packages: finviz
  Building wheel for finviz (setup.py) ... done
  Created wheel for finviz: filename=finviz-1.4.6-py3-none-any.whl size=17770 sha256=9c1cbdc29f7eb183c019f97e288f1c4f832d6407ca76b23a029bb5b24864772b
  Stored in directory: /private/var/folders/hx/r87bbj7d3zv14zqdwqw3vgz00000gn/T/pip-ephem-wheel-cache-rp4vaeuz/wheels/98/be/a9/27d7c97db12b1c29cc4e8d4d2c6647c51a80ce1738d38192b9
Successfully built finviz
Installing collected packages: finviz
  DEPRECATION: Configuring installation scheme with distutils config files is deprecated and will no longer work in the near future. If you are using a Homebrew or Linuxbrew Python, please see discussion at https://github.com/Homebrew/homebrew-core/issues/76621
DEPRECATION: Configuring installation scheme with distutils config files is deprecated and will no longer work in the near future. If you are using a Homebrew or Linuxbrew Python, please see discussion at https://github.com/Homebrew/homebrew-core/issues/76621
Successfully installed finviz-1.4.6

~$ python
Python 3.9.18 (main, Nov  2 2023, 16:52:00) 
[Clang 14.0.0 (clang-1400.0.29.202)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import finviz
>>> print(finviz.get_stock('aapl'))

{'Ticker': 'AAPL', 'Company': 'Apple Inc', 'Website': 'http://www.apple.com', 'Sector': 'Technology', 'Industry': 'Consumer Electronics', 'Country': 'USA', 'Exchange': 'NASD', 'Index': 'DJIA, NDX, S&P 500', 'P/E': '26.83', 'EPS (ttm)': '6.43', 'Insider Own': '0.09%', 'Shs Outstand': '15.55B', 'Perf Week': '-0.11%', 'Market Cap': '2662.65B', 'Forward P/E': '24.13', 'EPS next Y': '7.15', 'Insider Trans': '-5.05%', 'Shs Float': '15.43B', 'Perf Month': '-6.48%', 'Income': '100.91B', 'PEG': '2.44', 'EPS next Q': '1.51', 'Inst Own': '61.88%', 'Short Float': '0.73%', 'Perf Quarter': '-11.50%', 'Sales': '385.71B', 'P/S': '6.90', 'EPS this Y': '7.17%', 'Inst Trans': '-0.48%', 'Short Ratio': '1.86', 'Perf Half Y': '-0.86%', 'Book/sh': '4.79', 'P/B': '35.98', 'EPS growth next Y': '8.78%', 'ROA': '28.82%', 'Short Interest': '111.93M', 'Perf Year': '9.25%', 'Cash/sh': '4.73', 'P/C': '36.42', 'EPS next 5Y': '11.00%', 'ROE': '154.27%', '52W Range': '155.98 - 199.62', 'Perf YTD': '-10.44%', 'Dividend Est.': '1.01 (0.59%)', 'P/FCF': '24.92', 'EPS past 5Y': '15.55%', 'ROI': '59.65%', '52W High': '-13.62%', 'Beta': '1.28', 'Dividend TTM': '0.96 (0.56%)', 'Quick Ratio': '1.02', 'Sales past 5Y': '8.34%', 'Gross Margin': '45.03%', '52W Low': '10.55%', 'ATR (14)': '3.65', 'Dividend Ex-Date': 'Feb 09, 2024', 'Current Ratio': '1.07', 'EPS Y/Y TTM': '9.05%', 'Oper. Margin': '30.76%', 'RSI (14)': '41.53', 'Volatility (Week)': '2.36%', 'Volatility (Month)': '1.82%', 'Employees': '161000', 'Debt/Eq': '1.46', 'Sales Y/Y TTM': '-0.47%', 'Profit Margin': '26.16%', 'Recom': '2.18', 'Target Price': '199.20', 'Option/Short': 'Yes / Yes', 'LT Debt/Eq': '1.28', 'EPS Q/Q': '15.81%', 'Payout': '15.32%', 'Rel Volume': '1.81', 'Prev Close': '171.37', 'Sales Surprise': '1.35%', 'EPS Surprise': '3.77%', 'Sales Q/Q': '2.07%', 'Earnings': 'Feb 01 AMC', 'Avg Volume': '60.07M', 'Price': '172.43', 'SMA20': '-1.60%', 'SMA50': '-5.57%', 'SMA200': '-6.16%', 'Trades': '\r\n    ', '\r\n': 'Volume', '25,348,425': 'Change'}
>>>