-
Notifications
You must be signed in to change notification settings - Fork 0
/
scrape.py
158 lines (137 loc) · 4.55 KB
/
scrape.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
import selenium.webdriver as webdriver
from selenium.webdriver.common.keys import Keys
import time
f_list=['''true==function()
{
if( ((pmax)-(pmin))/(pl) >= 0.04 && (pf) == (pmin) && (pl) <=
(pmax)-((pmax)-(pmin))*0.05 && (pl) >= (pmax)-((pmax)-(pmin))*0.2)
{
return true;
}
else
{
return false;
}
}()''','''true==function()
{
if( ((pmax)-(pmin))/(pl) >= 0.04 && (pf) >=
((pmin)+((pmax)-(pmin))*0.05) && (pf) <= ((pmin)+((pmax)-(pmin))*0.2)
&& (pl) >= (pmax)-((pmax)-(pmin))*0.05)
{
return true;
}
else
{
return false;
}
}()''','''true==function()
{
if( ((pmax)-(pmin))/(pl) >= 0.04 && (pf) == (pmin) && (pl) <=
(pmax)-((pmax)-(pmin))*0.05 && (pl) >= (pmax)-((pmax)-(pmin))*0.2)
{
return true;
}
else
{
return false;
}
}()''']
driver = webdriver.Chrome('chromedriver.exe')
url='http://www.tsetmc.com/Loader.aspx?ParTree=15131F#'
driver.get(url)
def scrape(filter_list=None ,driver=None,bot=None,chat_id=None,f_name=None):
# add filter
e=driver.find_element_by_xpath('//*[@id="SettingsDesc"]/div[1]/a[7]')
e.click()
new_filter=driver.find_element_by_css_selector('#FilterIndex > div.awesome.black')
new_filter.click()
selected_filter=driver.find_elements_by_xpath('//*[@id="FilterIndex"]/div[1]')
selected_filter[0].click()
filter_area=driver.find_element_by_id('InputFilterCode')
# print(filter_list)
filter_area.send_keys(filter_list[0])
save=driver.find_element_by_css_selector('#FilterContent > div.awesome.blue')
save.click()
for i in range(len(filter_list)-1):
add_next_filter=driver.find_element_by_css_selector('#FilterIndex > div.awesome.black')
add_next_filter.click()
for i in range(1,len(filter_list)):
filter_btn=driver.find_element_by_css_selector('#FilterIndex > div:nth-child(%i)'%(i+2))
filter_btn.click()
filter_area=driver.find_element_by_id('InputFilterCode')
filter_area.send_keys(filter_list[i])
save=driver.find_element_by_css_selector('#FilterContent > div.awesome.blue')
save.click()
# return check_new(filter_list,bot=bot,chat_id=chat_id,f_name=f_name)
def check_new(filter_list,bot=None,chat_id=None,f_name=None,driver=None):
# fetch data
# global shared_namad,shared_data,shared_intersect
# global c
data=[]
namad=[]
for i in range(len(filter_list)):
filter_btn=driver.find_element_by_css_selector('#FilterIndex > div:nth-child(%i)'%(i+2))
filter_btn.click()
html=driver.page_source
from bs4 import BeautifulSoup
soup=BeautifulSoup(html,'html.parser')
a=soup.findAll('div',{'class':"{c}"})
namad_list=[]
n=[]
for tag in a:
d={}
d['namad']=tag.find('a').text
d['price']=tag.find('div',{'class':"t0c t0c3 ch0"}).text
n.append(d['namad'])
namad_list.append(d)
data.append(namad_list)
namad.append(n)
# intersection
init=namad[0]
for i in namad[1:]:
s=list(set(init)&set(i))
init=s
# init is result
print(init)
return init,data
# s=''
# for x in range(len(data)):
# s+='اطلاعات {0}'.format(f_name[x])
# s+='\n'
# for d in data[x]:
# s+=d['namad']
# s+='\t'
# s+=d['price']
# s+='\n'
# s+='------------------------------------\n'
# recheck(update=bot.getUpdates(),filter_list=filter_list,bot=bot,chat_id=chat_id,fname=f_name)
# def recheck(update,filter_list,bot,chat_id,fname):
# if update!=[]:
# print(update[0]['message']['text'])
# else:
# time.sleep(60)
# check_new(filter_list,bot,chat_id,fname)
def kill(driver=driver):
driver.quit()
if __name__=='__main__':
import sys
args=sys.argv
scrape(filter_list=args[0],driver=driver,bot=args[1],chat_id=args[2],f_name=args[3])
stack=[]
intersect,data=check_new(filter_list=args[0],bot=args[1],chat_id=args[2],f_name=args[3],driver=driver)
s=''
for x in range(len(data)):
s+='data of filter {0}'.format(args[3][x])
s+='\n'
for d in data[x]:
s+=d['namad']
s+='\t'
s+=d['price']
s+='\n'
s+='------------------------------------\n'
if len(stack)>1:
if stack[0]!=stack[1]:
args[1].sendMessage(args[2],s)
stack.pop(0)
else:
args[1].sendMessage(args[2],s)