mariostoev/finviz

Analyst Rating: Bug

Opened this issue · 3 comments

this still returns an empty string

finviz.get_analyst_price_targets("AAPL")

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

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

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]