/
parsex.py
219 lines (183 loc) · 6.36 KB
/
parsex.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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# scraper stuff
import datetime, binascii
import lxml.html
from datetime import datetime
import os
from bs4 import BeautifulSoup
import datetime
from datetime import date
import json
import feedparser
import time
import classes
# check a webpage for validity
####################################################################
def pagecheck(doc):
stat = 0
xstr ='Unauthorized'
if doc!=None:
if len(doc)>1024:
for word in str(doc).split(' '):
if word==xstr: stat = 1
break
return stat
# mon string to int
####################################################################
def mon_conv(date):
return{'Jan' : 1,'Feb' : 2,'Mar' : 3,'Apr' : 4,'May' : 5,'Jun' : 6,\
'Jul' : 7,'Aug' : 8,'Sep' : 9,'Oct' : 10,'Nov' : 11,'Dec' : 12}[date]
####################################################################
# Yahoo finance scraper
####################################################################
def yahoo(doc2):
doc = doc2.decode(encoding='UTF-8')
idata=[]
empty = ''
lempty = ['']
atype = 'Yahoo Finance'
info = ''
base = 'https://in.finance.yahoo.com'
timey = time.mktime(datetime.datetime.now().timetuple())
soup = BeautifulSoup(doc,"lxml")
gdata = soup.find_all("",{"class":"description"})
#tdata=[]
#ydata=[]
n = len(gdata)
for i in range(0,n):
j=0
#print i, gdata[i].text
#print i, gdata[i].contents
# get href link
lstr = str(gdata[i].contents)
#print lstr
soup2 = BeautifulSoup(lstr,"lxml")
for link in soup2.find_all('a'):
xlink = base+str(link.get('href'))
break
# get info, announ, atype
t1 = 'More'#+str(175)
t2 = 'More'
xstr = ''.join(str(gdata[i].text.encode('ascii', 'ignore')))
xstr2 = ''.join(xstr).split(t1)
#xstr = (str(gdata[i].text.encode('ascii', 'ignore').split(t1)).replace(t2,''))
#print i, xstr2
if xstr2!=['']:
#tdata.append(xstr2[1])# announ
#tdata.append(xstr2[0])# info
#tdata.append(timey) # time stamp
#tdata.append(xlink) # link
idata.append(classes.ticker(xstr2[1],atype,xlink,timey,xstr2[0],0))
#print i, xstr2[0]
#print i, xstr2[1]
if i==50: break # 50 articles in each page
#soup2 = BeautifulSoup(str(gdata))
#tables = soup2.find_all("tr")
idata.sort(key=lambda x: x.time, reverse=True)
return idata[0:classes.ticker.narticles]
####################################################################
# IIFL scraper - 1
####################################################################
# -----------------code removed-----------------------------------
####################################################################
# livemint scraper
####################################################################
def livemint(doc2,id):
doc = doc2.decode(encoding='UTF-8')
atype = 'livemint'+id
feed = feedparser.parse(doc)
i=0
idata = []
for post in feed.entries:
i=i+1
announ = str(post.title.encode('ascii', 'ignore'))
xlink = str(post.link.encode('ascii', 'ignore'))
timestamp = int(time.mktime(post.updated_parsed))
info = str(post.summary.encode('ascii', 'ignore')).split('>')
idata.append(classes.ticker(announ, atype, xlink, timestamp, info[1],0))
idata.sort(key=lambda x: x.time, reverse=True)
return idata[0:classes.ticker.narticles]
####################################################################
# mydigitalfc scraper
####################################################################
def mydigitalfc(doc2):
doc = doc2.decode(encoding='UTF-8')
atype = 'mydigitalfc'
feed = feedparser.parse(doc)
i=0
idata = []
for post in feed.entries:
i=i+1
announ = str(post.title.encode('ascii', 'ignore'))
xlink = str(post.link.encode('ascii', 'ignore'))
timestamp = int(time.mktime(post.updated_parsed))
info = str(post.summary.encode('ascii', 'ignore')).split('>')
idata.append(classes.ticker(announ, atype, xlink, timestamp, info[1],0))
idata.sort(key=lambda x: x.time, reverse=True)
return idata[0:classes.ticker.narticles]
####################################################################
# ibnlive scraper
####################################################################
# -----------------code removed-----------------------------------
####################################################################
# MControl scraper
####################################################################
def mc(doc2):
doc = doc2.decode(encoding='UTF-8')
atype = 'mc'
feed = feedparser.parse(doc)
i=0
idata = []
for post in feed.entries:
i=i+1
announ = str(post.title.encode('ascii', 'ignore'))
xlink = str(post.link.encode('ascii', 'ignore'))
timestamp = int(time.mktime(post.updated_parsed))
info = str(post.summary.encode('ascii', 'ignore')).split('>')
idata.append(classes.ticker(announ, atype, xlink, timestamp, info[1],0))
idata.sort(key=lambda x: x.time, reverse=True)
return idata[0:classes.ticker.narticles]
####################################################################
# Economic times scraper
####################################################################
def etimes(doc2):
doc = doc2.decode(encoding='UTF-8')
atype = 'etimes'
feed = feedparser.parse(doc)
i=0
idata = []
for post in feed.entries:
i=i+1
announ = str(post.title.encode('ascii', 'ignore'))
xlink = str(post.link.encode('ascii', 'ignore'))
timestamp = int(time.mktime(post.updated_parsed))
info = str(post.summary.encode('ascii', 'ignore')).split('>')
#print announ
#print xlink
#print timestamp, util.ts_conv(timestamp)
#print info[3]
idata.append(classes.ticker(announ, atype, xlink, timestamp, info[3],0))
#print i, announ, atype, xlink, timestamp, info[3]
#pline2()
#pline()
idata.sort(key=lambda x: x.time, reverse=True)
return idata[0:classes.ticker.narticles]
####################################################################
# SMERA scraper
####################################################################
def smera(doc2):
doc = doc2.decode(encoding='UTF-8')
atype = 'SMERA'
soup = BeautifulSoup(doc,"lxml")
gdata = soup.find_all("",{"class":"company"})
idata=[]
for items in gdata:
announ = str(items.text).replace('View','')
xlink = str(items.contents[1].get('href'))
timestamp = int(time.time())
info = ''
idata.append(classes.ticker(announ, atype, xlink, timestamp, info,0))
if(len(idata)==0): idata = None
idata.sort(key=lambda x: x.time, reverse=True)
return idata[0:classes.ticker.narticles]
.ticker.narticles]
# ------------- Rest of code deleted -------------------------------------