|
1 |
| -from finviz.async_connector import Connector |
2 | 1 | from lxml import html
|
3 | 2 | from lxml import etree
|
4 |
| -import requests |
5 |
| -import urllib3 |
6 |
| -import os |
7 |
| - |
8 |
| -urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) |
9 |
| - |
10 |
| -TABLE = { |
11 |
| - 'Overview': '110', |
12 |
| - 'Valuation': '120', |
13 |
| - 'Ownership': '130', |
14 |
| - 'Performance': '140', |
15 |
| - 'Custom': '150', |
16 |
| - 'Financial': '160', |
17 |
| - 'Technical': '170' |
18 |
| -} |
19 |
| - |
20 |
| - |
21 |
| -def http_request(url, payload=None): |
22 |
| - |
23 |
| - if payload is None: |
24 |
| - payload = {} |
25 |
| - |
26 |
| - content = requests.get(url, params=payload, verify=False) |
27 |
| - content.raise_for_status() # Raise HTTPError for bad requests (4xx or 5xx) |
28 |
| - |
29 |
| - return content, content.url |
| 3 | +import finviz.request_functions as send |
| 4 | +import finviz.scraper_functions as scrape |
30 | 5 |
|
31 | 6 |
|
32 | 7 | class Screener(object):
|
@@ -57,38 +32,15 @@ def __init__(self, tickers=None, filters=None, rows=None, order='', signal='', t
|
57 | 32 |
|
58 | 33 | def to_csv(self, directory=None):
|
59 | 34 |
|
60 |
| - from save_data import export_to_csv |
| 35 | + from .save_data import export_to_csv |
61 | 36 |
|
62 | 37 | if directory is None:
|
| 38 | + |
| 39 | + import os |
63 | 40 | directory = os.getcwd()
|
64 | 41 |
|
65 | 42 | export_to_csv(self.headers, self.data, directory)
|
66 | 43 |
|
67 |
| - def __get_total_rows(self): |
68 |
| - |
69 |
| - total_element = self.page_content.cssselect('td[width="140"]') |
70 |
| - self.rows = int(etree.tostring(total_element[0]).decode("utf-8").split('</b>')[1].split(' ')[0]) |
71 |
| - |
72 |
| - def __get_page_urls(self): |
73 |
| - |
74 |
| - try: |
75 |
| - total_pages = int([i.text.split('/')[1] for i in self.page_content.cssselect('option[value="1"]')][0]) |
76 |
| - except IndexError: # No results found |
77 |
| - return None |
78 |
| - |
79 |
| - urls = [] |
80 |
| - |
81 |
| - for page_number in range(1, total_pages + 1): |
82 |
| - |
83 |
| - sequence = 1 + (page_number - 1) * 20 |
84 |
| - |
85 |
| - if sequence - 20 <= self.rows < sequence: |
86 |
| - break |
87 |
| - else: |
88 |
| - urls.append(self.url + '&r={}'.format(str(sequence))) |
89 |
| - |
90 |
| - self.page_urls = urls |
91 |
| - |
92 | 44 | def __get_table_headers(self):
|
93 | 45 |
|
94 | 46 | first_row = self.page_content.cssselect('tr[valign="middle"]')
|
@@ -137,27 +89,33 @@ def parse_row(line):
|
137 | 89 |
|
138 | 90 | def __search_screener(self):
|
139 | 91 |
|
| 92 | + table = { |
| 93 | + 'Overview': '110', |
| 94 | + 'Valuation': '120', |
| 95 | + 'Ownership': '130', |
| 96 | + 'Performance': '140', |
| 97 | + 'Custom': '150', |
| 98 | + 'Financial': '160', |
| 99 | + 'Technical': '170' |
| 100 | + } |
| 101 | + |
140 | 102 | payload = {
|
141 |
| - 'v': TABLE[self.table], |
| 103 | + 'v': table[self.table], |
142 | 104 | 't': ','.join(self.tickers),
|
143 | 105 | 'f': ','.join(self.filters),
|
144 | 106 | 'o': self.order,
|
145 | 107 | 's': self.signal
|
146 | 108 | }
|
147 | 109 |
|
148 |
| - self.page_content, self.url = http_request('https://finviz.com/screener.ashx', payload) |
| 110 | + self.page_content, self.url = send.http_request('https://finviz.com/screener.ashx', payload) |
149 | 111 | self.page_content = html.fromstring(self.page_content.text) # Parses the page with the default lxml parser
|
150 | 112 |
|
151 | 113 | self.__get_table_headers()
|
152 | 114 |
|
153 | 115 | if self.rows is None:
|
154 |
| - self.__get_total_rows() |
155 |
| - |
156 |
| - self.__get_page_urls() |
| 116 | + self.rows = scrape.get_total_rows(self.page_content) |
157 | 117 |
|
158 |
| - if self.page_urls is None: |
159 |
| - raise Exception("No results matching the criteria: {}" |
160 |
| - .format(self.url.split('?', 1)[1])) |
| 118 | + self.page_urls = scrape.get_page_urls(self.page_content, self.rows, self.url) |
161 | 119 |
|
162 |
| - async_connector = Connector(self.__get_table_data, self.page_urls) |
| 120 | + async_connector = send.Connector(self.__get_table_data, self.page_urls) |
163 | 121 | self.data = async_connector.run_connector()
|
0 commit comments