mariostoev/finviz

No results found for query when using Screener

Opened this issue ยท 5 comments

My code:

from finviz.screener import Screener
import pandas as pd

stock_list = Screener(filters=['idx_sp500'], table='Overview')

Results in this error:

Traceback (most recent call last):
  File "venv/test.py", line 4, in <module>
    stock_list = Screener(filters=['idx_sp500'], table='Overview')
  File "venv/lib/python3.10/site-packages/finviz/screener.py", line 128, in __init__
    self.data = self.__search_screener()
  File "venv/lib/python3.10/site-packages/finviz/screener.py", line 437, in __search_screener
    self._rows = self.__check_rows()
  File "venv/lib/python3.10/site-packages/finviz/screener.py", line 407, in __check_rows
    raise NoResults(self._url.split("?")[1])
finviz.helper_functions.error_handling.NoResults: No results found for query: v=111&t=&f=idx_sp500&o=&s=&c=

It says No results found for query: v=111&t=&f=idx_sp500&o=&s=&c=, but going to https://finviz.com/screener.ashx?v=111&t=&f=idx_sp500&o=&s=&c= you can see the query works fine. Not sure what's causing it to believe no results were found.

I too am seeing the same error, i.e. nothing is returned.
I have tried many filters, and the response is the same: "No results found for query".

Same here - it worked last week, but I'm getting zero rows from the screener today for several sets of criteria.

This line no longer works - it looks like they changed the width to 100%. It's harder to find a good selector now.
total_element = page_content.cssselect('td[width="128"]')

Got it working by changing the CSS selector from td[width="128"] to div[id="screener-total"] in finviz/helper_functions/scraper_functions.py on line 44

I tried that, still not working.

total_element = page_content.cssselect('div[id="screener-total"]')

Update: I found that when combined with the fix for #166 in screener.py it started working. Both changes are needed to make this work.
` def __get_table_headers(self):
""" Private function used to return table headers. """

    headers = self._page_content.cssselect(
        'tr[valign="middle"]')[0].xpath("td//text()")
    # remove \r\n in headers
    return list(filter(lambda x: x.strip(), headers))`

My final more universal version that works at the moment:

def get_total_rows(page_content):
    """ Returns the total number of rows(results). """

    options=[('class="count-text whitespace-nowrap">#1 / ',' Total</div>'),('class="count-text">#1 / ',' Total</td>')]
    page_text = str(html.tostring(page_content))
    for option_beg,option_end in options:
        if option_beg in page_text:
            total_number = page_text.split(option_beg)[1].split(option_end)[0]
            try:
                return int(total_number)
            except ValueError:
                return 0
    return 0