mariostoev / finviz

Unofficial API for finviz.com
MIT License
1.06k stars 277 forks source link

Analyst Rating: Bug #145

Open bruce1095 opened 1 year ago

bruce1095 commented 1 year ago

this still returns an empty string

finviz.get_analyst_price_targets("AAPL")

bruce1095 commented 1 year ago

***returns an empty list: finviz.get_analyst_price_targets("AAPL") Out[77]:

glitchawy commented 1 year ago

same error here I found a closed issue for this but I didn't understand it #142

glitchawy commented 1 year ago

Just found the cause of the problem : Firstly the try and except statement always made the function pass until it return an empty list .. so I firstly removed it and then i found that the function was trying to convert the first row of the table that has strings using strptime() function .. now you can make a counter to pass the first row and start from the second like this : `

analyst_price_targets = []
get_page(ticker)
page_parsed = STOCK_PAGE[ticker]
table = page_parsed.cssselect(
    'table[class="js-table-ratings fullview-ratings-outer"]'
)[0]
counter = 0
for row in table:
    counter+=1
    if counter == 1 :
        pass
    else :
        rating = row.xpath("td//text()")
        rating = [val.replace("→", "->").replace("$", "") for val in rating if val != "\n"]
        rating[0] = datetime.strptime(rating[0], "%b-%d-%y").strftime("%Y-%m-%d")

        data = {
            "date": rating[0],
            "category": rating[1],
            "analyst": rating[2],
            "rating": rating[3],
        }
        if len(rating) == 5:
            if "->" in rating[4]:
                rating.extend(rating[4].replace(" ", "").split("->"))
                del rating[4]
                data["target_from"] = float(rating[4])
                data["target_to"] = float(rating[5])
            else:
                data["target"] = float(rating[4])

        analyst_price_targets.append(data)

return analyst_price_targets[:last_ratings]