From 67e316db05a94b68efa9b87bf65b677917a73770 Mon Sep 17 00:00:00 2001 From: Mario Stoev Date: Sun, 20 Jan 2019 13:18:05 +0200 Subject: [PATCH] Added get_stock and get_insider --- finviz/__init__.py | 5 ++++- finviz/main_func.py | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 finviz/main_func.py diff --git a/finviz/__init__.py b/finviz/__init__.py index 905d7ce..acd6bfc 100644 --- a/finviz/__init__.py +++ b/finviz/__init__.py @@ -1 +1,4 @@ -from finviz.screener import Screener \ No newline at end of file +from finviz.screener import Screener +from finviz.portfolio import Portfolio +from finviz.main_func import get_stock +from finviz.main_func import get_insider \ No newline at end of file diff --git a/finviz/main_func.py b/finviz/main_func.py new file mode 100644 index 0000000..8626d40 --- /dev/null +++ b/finviz/main_func.py @@ -0,0 +1,40 @@ +from finviz.helper_functions.request_functions import http_request_get + +STOCK_URL = 'https://finviz.com/quote.ashx' + + +def get_stock(ticker): + """ + Returns a dictionary containing stock data. + + :param ticker: stock symbol + :type ticker: str + :return dict + """ + + data = {} + page_parsed, _ = http_request_get(url=STOCK_URL, payload={'t': ticker}, parse=True) + 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): + if column % 2 == 0: + data[row[column]] = row[column + 1] + + return data + + +def get_insider(ticker): + """ + Returns a list of sets containing all recent insider transactions. + + :param ticker: stock symbol + :return: + """ + + page_parsed, _ = http_request_get(url=STOCK_URL, payload={'t': ticker}, parse=True) + table = page_parsed.cssselect('table[class="body-table"]')[0] + headers = table[0].xpath('td//text()') + data = [dict(zip(headers, row.xpath('td//text()'))) for row in table[1:]] + + return data