-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetching stock price.py
More file actions
executable file
·131 lines (91 loc) · 2.51 KB
/
fetching stock price.py
File metadata and controls
executable file
·131 lines (91 loc) · 2.51 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import urllib
import re
import requests
symbols = ["aapl", "goog", "ba", "nflx", "ge", "ttm", "bhel.ns", "wmt"]
i=0
while i < len(symbols):
url = "http://finance.yahoo.com/q?s="+ symbols[i]
regex = '<span id="yfs_l84_' +symbols[i]+'">(.+?)</span>'
pattern = re.compile(regex)
htmlfile = requests.get(url)
htmltext = htmlfile.text
price = re.findall(pattern, htmltext)
print("price of ", symbols[i], "is ", price)
i+=1
'''
#http://www.bloomberg.com/markets/chart/data/1D/AAPL:US
import urllib
import re
import requests
import csv
symbols = []
csvfile = csv.reader(open("companylist.csv", "r"))
for row in csvfile:
symbols.append(((row[0]).lower()))
i = 0
while i < len(symbols):
url = "http://finance.yahoo.com/q?s=" + symbols[i]
regex = '<span id="yfs_l84_' + symbols[i] + '">(.+?)</span>'
pattern = re.compile(regex)
htmlfile = requests.get(url)
htmltext = htmlfile.text
price = re.findall(pattern, htmltext)
print("price of ", symbols[i], "is ", price)
i += 1
'''
## with multi threading:::: :::::::::::::::::::::::::::::::::
'''
import urllib
import re
import requests
import csv
from threading import Thread
symbols = []
csvfile = csv.reader(open("companylist.csv", "r"))
for row in csvfile:
symbols.append(((row[0]).lower()))
def th(ur):
base = "http://finance.yahoo.com/q?s=" + ur
i = 0
regex = '<span id="yfs_l84_' + ur + '">(.+?)</span>'
pattern = re.compile(regex)
htmlfile = requests.get(base)
htmltext = htmlfile.text
price = re.findall(pattern, htmltext)
print("price of ", ur , "is ", price)
i += 1
threadlist = []
for u in symbols:
t = Thread(target=th,args=(u,))
t.start()
threadlist.append(t)
for b in threadlist:
b.join()
'''
#using web srawler re.search
'''
import re
import requests
import csv
symbols = []
csvfile = csv.reader(open("companylist.csv", "r"))
for row in csvfile:
symbols.append(((row[0]).lower()))
symbols.pop(0)
for symb in symbols:
url = "http://www.google.com/finance?q="
url2 = url+symb
data = requests.get(url2).text
m = re.search('span class="pr"', data)
if(m==None):
continue
start = m.end()
end = start+50
data1 = data[start:end]
m= re.search(('">'),data1)
start = m.end()
data2 = data1[start:(start+14)]
m = re.search('/',data2)
new_end=(m.start())-1
print(data2[:new_end])
'''