-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscript_python3.py
151 lines (127 loc) · 6.04 KB
/
script_python3.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
import json
import traceback
import requests
import re
import datetime
from bs4 import BeautifulSoup
from pymongo import MongoClient
'''
Class to scrap the economic time data
'''
class EconomicScrapper(object):
def __init__(self):
with open("./config.json", "r") as fp:
self.config_dct = json.load(fp)
try:
self.client = MongoClient('localhost', 27017)
print ("connected to mongodb")
except Exception as e:
print ("exception occured while connecting to database",e)
# db = self.client.scrap_econo
'''
Main crawler function
'''
def crawler(self):
result = {}
try:
headers = {
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36"
}
result_content = requests.get(self.config_dct["target_url"], headers= headers, proxies = self.config_dct["proxy_config"], timeout = 5 )
if result_content.status_code == 200:
if result_content.content:
crawled_data = result_content.content
rgex_search = re.search(b'ajaxResponse\((.*)\)(\r)?(\n)?', crawled_data)
if rgex_search:
result = json.loads(rgex_search.group(1))
# print json.dumps(result, indent=3)
else:
print ("Data is blank.")
else:
print ("*"*50, " Site is blocking the crawling activity. Hit again.\n Error Code", result_content.status_code, "*"*50)
except Exception as e:
print ("Exception in crawler for crawling: ", e)
return result
def crawlerNseBse(self,co_id):
result = {}
try:
headers = {
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36"
}
result_content = requests.get("https://json.bselivefeeds.indiatimes.com/ET_Community/companypagedata?companyid="+str(co_id)+"&companytype=&callback=ets.hitMarket",
headers= headers, proxies = self.config_dct["proxy_config"], timeout = 5 )
if result_content.status_code == 200:
if result_content.content:
crawled_data = result_content.content
rgex_search = re.search(b'ets.hitMarket\((.*)\)(\r)?(\n)?', crawled_data)
if rgex_search:
result = json.loads(rgex_search.group(1))
# print json.dumps(result, indent=3)
else:
print ("Data is blank.")
else:
print ("*"*50, " Site is blocking the crawling activity. Hit again.\n Error Code", result_content.status_code, "*"*50)
except Exception as e:
print ("Exception in crawler for crawling: ", e)
# print result
return result
'''
Dump in mongodb
'''
def dump_db(self, data_to_dump):
companyId = []
db_name = self.config_dct["db_name"]
db_ref = self.client[db_name]
collection = db_ref["historic_data"]
data_to_insert = {}
try:
if data_to_dump:
for items in data_to_dump['searchresult']:
data_to_insert = items
co_id = items['companyid']
date_str = items['xdividenddatestr'].strip()
format_str = '%d-%m-%Y'
dividendDate = datetime.datetime.strptime(date_str,format_str)
# print co_id
print (type(dividendDate))
bse_nse_data = self.crawlerNseBse(co_id)
bseNseJson = bse_nse_data['bseNseJson']
bsePrice = ''
nsePrice = ''
bseDividend = ''
nseDividend = ''
if len(bseNseJson) > 0:
for item_d in bseNseJson:
if 'segment' in item_d:
if item_d['segment'] == 'BSE':
print ("bse found")
bsePrice = item_d['lastTradedPrice']
bseDividend = float(items['dividendvalue']) / float(bsePrice)
else:
print ("nse found")
nsePrice = item_d['lastTradedPrice']
nseDividend = float(items['dividendvalue']) / float(nsePrice)
else:
bsePrice = ''
nsePrice = ''
bseNseJson = []
bseDividend = ''
nseDividend = ''
data_to_insert['dividendDate'] = dividendDate
data_to_insert['bseNseJson'] = bseNseJson
data_to_insert['bsePrice'] = bsePrice
data_to_insert['nsePrice'] = nsePrice
data_to_insert['bseDividend'] = bseDividend
data_to_insert['nseDividend'] = nseDividend
collection.update(
{
'companyid':items['companyid']
},data_to_insert,upsert=True)
print ("Data inserted successfully in historic_data of data_center db")
except Exception as e:
print ("Exception in mongodb dump operation", e)
if __name__ == '__main__':
obj = EconomicScrapper()
return_data = obj.crawler()
obj.dump_db(return_data)
# // "https://mfapps.indiatimes.com/etcal/currencycontroller?pagesize=25&pid=58&pageno=1&sortby=mcap&sortorder=asc&year=2017&callback=ajaxResponse",