-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIPOCrawler.py
48 lines (43 loc) · 1.49 KB
/
IPOCrawler.py
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
from urllib2 import Request, urlopen
from bs4 import BeautifulSoup
import DBHelper
import MyLogger
from datetime import datetime
import IPOHelper
req = Request("https://www.chittorgarh.com/ipo/ipo_list.asp?a=mainline", headers={'User-Agent': 'Mozilla/5.0'})
def refreshData():
page = urlopen(req).read()
soup = BeautifulSoup(page, 'html.parser')
x = soup.find('table', {"class": "table-bordered" })
headers = x.find_all('th')
indexheaders = []
for header in headers:
indexheaders.append(header.get_text())
indexheaders.append('link')
alldata = []
rows = x.find_all('tr')
for row in rows:
cols = row.find_all('td')
if len(cols)==0:
continue
link = row.find('a').get('href')
rowdata = []
for col in cols:
datatoInsert = col.get_text().upper().replace("'","") #replcae single quote to handle sql injection
try:
d = datetime.strptime(datatoInsert, "%b %d, %Y")
datatoInsert = d.strftime('%Y-%m-%d')
except:
#dummt stmnt
a=10;
rowdata.append(datatoInsert)
rowdata.append(link)
alldata.append(rowdata)
#MyLogger.log(indexheaders)
#for row in alldata:
# MyLogger.log(row)
#MyLogger.log('check for table existance')
#if not DBHelper.isTableExist():
#MyLogger.log("table not exists")
#DBHelper.createTable()
return IPOHelper.insertNewIPOs(alldata)