Skip to content

Commit

Permalink
Minor bug fixes (#123)
Browse files Browse the repository at this point in the history
* Fixed existing field formats, improved get_insider(), added Website field
  • Loading branch information
d3an authored Mar 2, 2022
1 parent 1c421ac commit 2b1d04c
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 12 deletions.
33 changes: 25 additions & 8 deletions finviz/main_func.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from datetime import datetime

from lxml import etree

from finviz.helper_functions.request_functions import http_request_get
from finviz.helper_functions.scraper_functions import get_table

Expand Down Expand Up @@ -35,13 +37,25 @@ def get_stock(ticker):
fields = [f.text_content() for f in title.cssselect('a[class="tab-link"]')]
data = dict(zip(keys, fields))

company_link = title.cssselect('a[class="tab-link"]')[0].attrib["href"]
data["Website"] = company_link if company_link.startswith("http") else None

all_rows = [
row.xpath("td//text()")
for row in page_parsed.cssselect('tr[class="table-dark-row"]')
]

for row in all_rows:
for column in range(0, 11, 2):
if row[column] == "EPS next Y" and "EPS next Y" in data.keys():
data["EPS growth next Y"] = row[column + 1]
continue
elif row[column] == "Volatility":
vols = row[column + 1].split()
data["Volatility (Week)"] = vols[0]
data["Volatility (Month)"] = vols[1]
continue

data[row[column]] = row[column + 1]

return data
Expand All @@ -64,7 +78,11 @@ def get_insider(ticker):

table = outer_table[0]
headers = table[0].xpath("td//text()")
data = [dict(zip(headers, row.xpath("td//text()"))) for row in table[1:]]

data = [dict(zip(
headers,
[etree.tostring(elem, method="text", encoding="unicode") for elem in row]
)) for row in table[1:]]

return data

Expand All @@ -84,12 +102,12 @@ def get_news(ticker):
if len(news_table) == 0:
return []

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

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

if len(raw_timestamp) > 8:
parsed_timestamp = datetime.strptime(raw_timestamp, "%b-%d-%y %I:%M%p")
Expand Down Expand Up @@ -161,14 +179,14 @@ def get_analyst_price_targets(ticker, last_ratings=5):

for row in table:
rating = row.xpath("td//text()")
rating = [val.replace("→", "->").replace("$", "") for val in rating if val != '\n']
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],
"date": rating[0],
"category": rating[1],
"analyst": rating[2],
"rating": rating[3],
"analyst": rating[2],
"rating": rating[3],
}
if len(rating) == 5:
if "->" in rating[4]:
Expand All @@ -181,7 +199,6 @@ def get_analyst_price_targets(ticker, last_ratings=5):

analyst_price_targets.append(data)
except Exception as e:
# print("-> Exception: %s parsing analysts' ratings for ticker %s" % (str(e), ticker))
pass

return analyst_price_targets[:last_ratings]
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "finviz"
version = "1.4.3"
version = "1.4.4"
description = "Unofficial API for finviz.com"
authors = ["Mario Stoev <bg.mstoev@gmail.com>", "James Bury <jabury@sympatico.ca>"]
license = "MIT"
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
finviz~=1.4.2
finviz~=1.4.4
beautifulsoup4~=4.9.3
requests~=2.25.1
aiohttp~=3.7.4
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
setup(
name="finviz",
packages=["finviz", "finviz.helper_functions"],
version="1.4.3",
version="1.4.4",
license="MIT",
description="Unofficial API for FinViz.com",
author="Mario Stoev, James Bury",
author_email="bg.mstoev@gmail.com, jabury@sympatico.ca",
url="/~https://github.com/mariostoev/finviz",
download_url="/~https://github.com/mariostoev/finviz/archive/1.4.3.tar.gz",
download_url="/~https://github.com/mariostoev/finviz/archive/1.4.4.tar.gz",
keywords=["finviz", "api", "screener", "finviz api", "charts", "scraper"],
install_requires=[
"lxml",
Expand Down

0 comments on commit 2b1d04c

Please sign in to comment.