-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrawler.py
More file actions
51 lines (41 loc) · 1.28 KB
/
crawler.py
File metadata and controls
51 lines (41 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import requests
from lxml import html
class StocksItem:
def __init__(self):
self.symbol = None
self.ltp = None
self.open = None
self.low = None
self.previous_close = None
def parse_data(response):
result_data = []
tree = html.fromstring(response.content)
rows = tree.xpath('//*[@id="headFixed"]/tbody/tr')
for row in rows:
data = [item.strip().replace('"', "").replace(",", "") for item in row.xpath('td//text()') if item.strip()]
symbol = row.xpath('td/a/text()')[0]
item = StocksItem()
item.symbol = symbol
item.ltp = data[1]
item.open = data[-5]
item.low = data[-3]
item.previous_close = data[-1]
result_data.append(item)
return result_data
def main():
url = "https://www.sharesansar.com/live-trading"
response = requests.get(url)
if response.status_code == 200:
result_data = parse_data(response)
for item in result_data:
print(item.__dict__)
def crawl():
url = "https://www.sharesansar.com/live-trading"
response = requests.get(url)
if response.status_code == 200:
result_data = parse_data(response)
return result_data
else:
return False
if __name__ == "__main__":
main()