-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultiThread.py
More file actions
42 lines (31 loc) · 947 Bytes
/
multiThread.py
File metadata and controls
42 lines (31 loc) · 947 Bytes
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
from threading import Thread
import urllib2
import re
import os
'''
A multithreaded share price retrieval
'''
# Chage working directory
direct = "C:\\Python2.7\\PythonWebScraping\\7_MultiThread"
os.chdir(direct)
# Stringify words
symbolsList = open("ASX_Top100.txt").read()
symbolsList = symbolsList.split("\n")
print symbolsList
def thread(shareCode):
pageUrl = "https://au.finance.yahoo.com/q?s=" + shareCode + ".AX"
htmlfile = urllib2.urlopen(pageUrl)
htmltext = htmlfile.read()
regex = '<span id="yfs_l84_'+shareCode.lower()+'.ax">(.+?)</span>'
pattern = re.compile(regex)
results = re.findall(pattern,htmltext)
print "the price of " + str(shareCode) + " is " + str(results[0])
threadList = []
for symbol in symbolsList:
print symbol
target = Thread(target=thread,args=(symbol,))
target.start()
threadList.append(target)
#Join threads back
for b in threadList:
b.join()