Code Monkey home page Code Monkey logo

Comments (9)

AGG2017 avatar AGG2017 commented on July 20, 2024 2

No, the final result for the function get_news should be:

def get_news(ticker):
    """
    Returns a list of sets containing news headline and url

    :param ticker: stock symbol
    :return: list
    """

    get_page(ticker)
    page_parsed = STOCK_PAGE[ticker]
    news_table = page_parsed.cssselect('table[id="news-table"]')

    if len(news_table) == 0:
        return []

    rows = news_table[0].xpath("./tr[not(@id)]")

    results = []
    date = None
    for row in rows:
        raw_timestamp = str(row.xpath("./td")[0].xpath("text()")[0][0:-2]).strip()

        if len(raw_timestamp) > 8:
            if 'Today' in raw_timestamp:
                parsed_timestamp = datetime.now()
            else:
                parsed_timestamp = datetime.strptime(raw_timestamp, "%b-%d-%y %I:%M%p")
            date = parsed_timestamp.date()
        else:
            parsed_timestamp = datetime.strptime(raw_timestamp, "%I:%M%p").replace(
                year=date.year, month=date.month, day=date.day)

        results.append((
            parsed_timestamp.strftime("%Y-%m-%d %H:%M"),
            row.xpath("./td")[1].cssselect('a[class="tab-link-news"]')[0].xpath("text()")[0],
            row.xpath("./td")[1].cssselect('a[class="tab-link-news"]')[0].get("href"),
            row.xpath("./td")[1].cssselect('div[class="news-link-right"] span')[0].xpath("text()")[0][1:]
        ))

    return results

I just test this function to get the AAPL news (the same example from the front page) and it works. And restart the Python session after applying the fix, otherwise the cached old one will be used if already it is imported.

from finviz.

AGG2017 avatar AGG2017 commented on July 20, 2024

Instead of the real date they put Today and fast fix at line 113 in main_func.py should be:

            if 'Today' in raw_timestamp:
                parsed_timestamp = datetime.now()
            else:
                parsed_timestamp = datetime.strptime(raw_timestamp, "%b-%d-%y %I:%M%p")
            date = parsed_timestamp.date()

Hopefully they didn't use Yesterday and so on. I didn't find news from yesterday to test it but for the one from today it works. It needs more work if you want to use the exact time from today, not the time of now.

Also I see you have white space in the date/time string. You should also implement the fix number 2 from my suggested fixes in #182.

from finviz.

AGG2017 avatar AGG2017 commented on July 20, 2024

Any feedback? Is it working for you now or you just don't know how to apply the fix?

from finviz.

wj210 avatar wj210 commented on July 20, 2024

Hi, i got the same error as well.
`results = []
date = None
for row in rows:
# raw_timestamp = row.xpath("./td")[0].xpath("text()")[0][0:-2]
raw_timestamp = str(row.xpath("./td")[0].xpath("text()")[0][0:-2]).strip()

    # if len(raw_timestamp) > 8:
    #     parsed_timestamp = datetime.strptime(raw_timestamp, "%b-%d-%y %I:%M%p")
    #     date = parsed_timestamp.date()
    # else:
    #     parsed_timestamp = datetime.strptime(raw_timestamp, "%I:%M%p").replace(
    #         year=date.year, month=date.month, day=date.day)
    if 'Today' in raw_timestamp:
        parsed_timestamp = datetime.now()
    else:
        parsed_timestamp = datetime.strptime(raw_timestamp, "%b-%d-%y %I:%M%p")
    date = parsed_timestamp.date()

    results.append((
        parsed_timestamp.strftime("%Y-%m-%d %H:%M"),
        row.xpath("./td")[1].cssselect('a[class="tab-link-news"]')[0].xpath("text()")[0],
        row.xpath("./td")[1].cssselect('a[class="tab-link-news"]')[0].get("href"),
        row.xpath("./td")[1].cssselect('div[class="news-link-right"] span')[0].xpath("text()")[0][1:]
    ))

return results`

Is this the correct fix?

from finviz.

AGG2017 avatar AGG2017 commented on July 20, 2024

Still no feedback to just say it works, thanks or it doesn't work and to provide the error information. No motivation to waste more of my valuable time on this project when the one asking for help doesn't want to spend even 10 seconds for a simple feedback. You should wait for the official fix releases one day. Sorry.

from finviz.

scottstables avatar scottstables commented on July 20, 2024

Apologies - I can confirm that the fix did indeed work.

from finviz.

vikyw89 avatar vikyw89 commented on July 20, 2024

this is my code:

from finviz import get_news
result = get_news(ticker="AAPL")
data_string = '\r\n            Jan-28-24 08:38PM\r\n      ', format = '%b-%d-%y %I:%M%p'

    def _strptime(data_string, format="%a %b %d %H:%M:%S %Y"):
        """Return a 2-tuple consisting of a time struct and an int containing
        the number of microseconds based on the input string and the
        format string."""
    
        for index, arg in enumerate([data_string, format]):
            if not isinstance(arg, str):
                msg = "strptime() argument {} must be str, not {}"
                raise TypeError(msg.format(index, type(arg)))
    
        global _TimeRE_cache, _regex_cache
        with _cache_lock:
            locale_time = _TimeRE_cache.locale_time
            if (_getlang() != locale_time.lang or
                time.tzname != locale_time.tzname or
                time.daylight != locale_time.daylight):
                _TimeRE_cache = TimeRE()
                _regex_cache.clear()
                locale_time = _TimeRE_cache.locale_time
            if len(_regex_cache) > _CACHE_MAX_SIZE:
                _regex_cache.clear()
            format_regex = _regex_cache.get(format)
            if not format_regex:
                try:
                    format_regex = _TimeRE_cache.compile(format)
                # KeyError raised when a bad format is found; can be specified as
                # \\, in which case it was a stray % but with a space after it
                except KeyError as err:
                    bad_directive = err.args[0]
                    if bad_directive == "\\":
                        bad_directive = "%"
                    del err
                    raise ValueError("'%s' is a bad directive in format '%s'" %
                                        (bad_directive, format)) from None
                # IndexError only occurs when the format string is "%"
                except IndexError:
                    raise ValueError("stray %% in format '%s'" % format) from None
                _regex_cache[format] = format_regex
        found = format_regex.match(data_string)
        if not found:
>           raise ValueError("time data %r does not match format %r" %
                             (data_string, format))
E           ValueError: time data '\r\n            Jan-28-24 08:38PM\r\n      ' does not match format '%b-%d-%y %I:%M%p'

/usr/lib/python3.11/_strptime.py:349: ValueError

from finviz.

vikyw89 avatar vikyw89 commented on July 20, 2024

fix from @AGG2017 works, why didn't it get merged to the master branch ?

from finviz.

estr95 avatar estr95 commented on July 20, 2024

I applied the fix from @AGG2017, but am getting a different error message now, so was wondering if something changed more recently.

Using the example:
finviz.get_news('AAPL')

The following is returned:
ValueError: time data '03:15' does not match format '%I:%M%p'

By chance, does anyone have a new fix for the function get_news to resolve this error ?

Very new to this and not sure what this error message means. Any help would be greatly appreciated!

from finviz.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.