-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaffiliate_net_work.py
251 lines (226 loc) · 11.2 KB
/
affiliate_net_work.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import re
import sys
import time
from venv import main
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from sqlalchemy import Column, String, create_engine, Integer
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.dialects import mysql
from sqlalchemy.sql import and_, asc, desc, or_
from comm.timestamp import get_now_timestamp
from comm.model import Affiliate_Network
from comm.config import sqlconn
def offervault(browser, session):
total_page = 17
url_prefix = 'https://offervault.com/networks/?search=&page='
for page in range(1, total_page + 1):
url = url_prefix + str(page)
browser.get(url)
print("---Current Page: {0} ---".format(page))
main_handle = browser.current_window_handle
try:
# no.1
table = browser.find_element_by_css_selector('#offertable > tbody')
trs = table.find_elements_by_tag_name('tr')
for tr in trs:
td = tr.find_elements_by_tag_name('td')[0]
link = td.find_element_by_tag_name('a').get_attribute('href')
js = 'window.open(\"' + link + '\");'
browser.execute_script(js)
handles = browser.window_handles
browser.switch_to.window(handles[1]) # 切换标签页
affiliate_network_name = browser.find_element_by_css_selector(
'#__layout > div > section > div > div.contentspacer > div > div:nth-child(1) > div > div > div > div > div > div > div:nth-child(1) > h2'
).text.split(' - ')[-1]
# 看之前是否已经爬取过
rows = session.query(Affiliate_Network).filter(
Affiliate_Network.name.like(affiliate_network_name)).all()
if rows:
browser.close()
browser.switch_to.window(main_handle)
print("已经爬取过")
continue
# click JOIN button
time.sleep(3)
browser.find_element_by_css_selector(
'#__layout > div > section > div > div.contentspacer > div > div:nth-child(1) > div > div > div > div > div > div > div:nth-child(1) > a.offerdesc'
).click()
print("successfully click")
handles = browser.window_handles
browser.switch_to.window(handles[2]) # 切换标签页
affiliate_network_url = browser.current_url
affiliate_network = Affiliate_Network()
affiliate_network.name = affiliate_network_name
affiliate_network.url = affiliate_network_url
affiliate_network.domain = affiliate_network_url.split('/')[2]
print("affiliate_network_name: ", affiliate_network_name)
print("affiliate_network_url: ", affiliate_network_url)
print("affiliate_network_domain: ", affiliate_network.domain)
session.add(affiliate_network)
session.commit()
browser.close()
browser.switch_to.window(handles[1])
browser.close()
browser.switch_to.window(main_handle)
except Exception as error:
print(error)
try:
# no.2
print("Part 2.")
table = browser.find_element_by_css_selector('#index-page-networkstable > tbody')
trs = table.find_elements_by_tag_name('tr')
for tr in trs:
td = tr.find_elements_by_tag_name('td')[0]
link = td.find_element_by_tag_name('a').get_attribute('href')
js = 'window.open(\"' + link + '\");'
browser.execute_script(js)
handles = browser.window_handles
browser.switch_to.window(handles[1]) # 切换标签页
affiliate_network_name = browser.find_element_by_css_selector(
'#__layout > div > section > div > div.contentspacer > div > div:nth-child(1) > div > div > div > div > div > div > div:nth-child(1) > h2'
).text.split(' - ')[-1]
# 看之前是否已经爬取过
rows = session.query(Affiliate_Network).filter(
Affiliate_Network.name.like(affiliate_network_name)).all()
if rows:
browser.close()
browser.switch_to.window(main_handle)
print("已经爬取过")
continue
# click JOIN button
time.sleep(3) # 确保点到
browser.find_element_by_css_selector(
'#__layout > div > section > div > div.contentspacer > div > div:nth-child(1) > div > div > div > div > div > div > div:nth-child(1) > a.offerdesc'
).click()
print("successfully click")
handles = browser.window_handles
browser.switch_to.window(handles[2]) # 切换标签页
affiliate_network_url = browser.current_url
affiliate_network = Affiliate_Network()
affiliate_network.name = affiliate_network_name
affiliate_network.url = affiliate_network_url
affiliate_network.domain = affiliate_network_url.split('/')[2]
print("affiliate_network_name: ", affiliate_network_name)
print("affiliate_network_url: ", affiliate_network_url)
print("affiliate_network_domain: ", affiliate_network.domain)
session.add(affiliate_network)
session.commit()
browser.close()
browser.switch_to.window(handles[1])
browser.close()
browser.switch_to.window(main_handle)
except Exception as error:
print(error)
def odigger(browser, session):
try:
total_page = 25 # 先写死吧
for page in range(1, total_page + 1):
url = 'https://odigger.com/networks?page=' + str(page) + '&search='
print(url)
browser.get(url)
print("Page: ", page)
main_handle = browser.current_window_handle
links = browser.find_elements_by_css_selector('table#networks-table> tbody > tr > td > span >h1 > a')
for link in links:
affiliate_network_name = link.text
print("affiliate_network_name: ", affiliate_network_name)
# 看之前是否已经爬取过
rows = session.query(Affiliate_Network).filter(
Affiliate_Network.name.like(affiliate_network_name)).all()
if rows:
print("已经爬取过")
continue
link = link.get_attribute('href')
js = 'window.open(\"' + link + '\");'
browser.execute_script(js)
handles = browser.window_handles
browser.switch_to.window(handles[1]) # 切换标签页
# click JOIN button
browser.find_element_by_css_selector(
'#app > section > div > div > div.col-900 > div > div:nth-child(1) > div.net-page > div:nth-child(2) > div > a').click()
handles = browser.window_handles
browser.switch_to.window(handles[2]) # 切换标签页
affiliate_network_url = browser.current_url
affiliate_network = Affiliate_Network()
affiliate_network.name = affiliate_network_name
affiliate_network.url = affiliate_network_url
affiliate_network.domain = affiliate_network_url.split('/')[2]
session.add(affiliate_network)
session.commit()
browser.close()
browser.switch_to.window(handles[1])
browser.close()
browser.switch_to.window(main_handle)
except Exception as err:
print("Error: ", err)
def affpay(browser, session):
try:
total_page = 111
url_prefix = 'https://www.affpaying.com/affiliate-networks?page='
for page in range(1, total_page + 1):
url = url_prefix + str(page)
browser.get(url)
main_handle = browser.current_window_handle
items = browser.find_elements_by_css_selector(
'body > div.main.container > div.mx-auto.relative > div > div.mr-4.w-full.z-20 > div.panel.flex.flex-col.w-full.shadow.border-blue.border-t-4.bg-white.px-6 > div.flex-col.pb-4 > div.flex-col'
)
for item in items:
affiliate_network_name = item.find_element_by_tag_name('h2').text
# 看之前是否已经爬取过
rows = session.query(Affiliate_Network).filter(
Affiliate_Network.name.like(affiliate_network_name)).all()
if rows:
print("已经爬取过")
continue
link = item.find_element_by_tag_name('h2').find_element_by_tag_name('a').get_attribute('href')
js = 'window.open(\"' + link + '\");'
browser.execute_script(js)
handles = browser.window_handles
browser.switch_to.window(handles[1]) # 切换标签页
try:
affiliate_network_url = browser.find_element_by_css_selector(
'body > div.main.container > div.mx-auto.relative > div > div.mr-4.w-full.z-20 > div.px-5.bg-white.shadow.border-blue.border-t-3.mb-3.pb-5 > div.entity.flex.justify-between.py-3 > div.flex.flex-col > div.flex.items-center.border-t.border-dashed.mt-3.py-3.text-sm > a'
).get_attribute('href')
except Exception as err:
print("Network 已关闭")
affiliate_network_url = ''
if affiliate_network_url:
affiliate_network = Affiliate_Network()
affiliate_network.name = affiliate_network_name
affiliate_network.url = affiliate_network_url
affiliate_network.domain = affiliate_network_url.split('/')[2]
session.add(affiliate_network)
session.commit()
browser.close()
browser.switch_to.window(main_handle)
except Exception as err:
print("Error: ", err)
def main():
# 正常模式
browser = webdriver.Chrome()
browser.maximize_window()
# # headless模式
# option = webdriver.ChromeOptions()
# option.add_argument('--headless')
# option.add_argument("--window-size=1920,1080")
# browser = webdriver.Chrome(chrome_options=option)
browser.implicitly_wait(15)
engine = create_engine(sqlconn, echo=True, max_overflow=16)
DBSession = sessionmaker(bind=engine)
session = DBSession()
# offervault:
offervault(browser, session)
# # odigger 爬完了
# odigger(browser, session)
# # affpay 爬完了
# affpay(browser, session)
browser.close()
browser.quit()
session.close()
if __name__ == "__main__":
main()