-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.py
301 lines (264 loc) · 11.9 KB
/
test.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
import unittest
from selenium.webdriver.common.by import By
import crawlist as cl
from selenium.webdriver.remote.webdriver import WebDriver
import requests
from requests import HTTPError
from webdriver_manager.chrome import ChromeDriverManager
from selenium import webdriver as wd
from selenium.webdriver.chrome.service import Service
def getDriver(is_headless=False):
option = wd.ChromeOptions()
arguments = [
"no-sandbox",
"--disable-extensions",
'--disable-gpu',
'User-Agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36"',
"window-size=1920x3000",
"start-maximized",
'cache-control="max-age=0"'
"disable-blink-features=AutomationControlled"
]
for argument in arguments:
option.add_argument(argument)
if is_headless:
option.add_argument("--headless")
option.add_experimental_option('excludeSwitches', ['enable-automation'])
webdriver = wd.Chrome(service=Service(ChromeDriverManager().install()), options=option)
webdriver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
"source": """
Object.defineProperty(navigator, 'webdriver', {
get: () => false
})
"""
})
return webdriver
class TestCase(unittest.TestCase):
limit = 100
def test_01(self):
pager = cl.StaticRedirectPager(
uri="https://www.douban.com/doulist/893264/?start=0&sort=seq&playable=0&sub_type=",
uri_split="https://www.douban.com/doulist/893264/?start=%v&sort=seq&playable=0&sub_type=",
start=0,
offset=25)
selector = cl.CssSelector(pattern=".doulist-item")
analyzer = cl.AnalyzerPrettify(pager, selector)
res = []
for tr in analyzer(TestCase.limit):
print(tr)
res.append(tr)
print(len(res))
def test_02(self):
uris = ["https://www.douban.com/doulist/893264/?start=0&sort=seq&playable=0&sub_type=",
"https://www.douban.com/doulist/893264/?start=25&sort=seq&playable=0&sub_type=",
"https://www.douban.com/doulist/893264/?start=50&sort=seq&playable=0&sub_type=",
"https://www.douban.com/doulist/893264/?start=75&sort=seq&playable=0&sub_type="]
pager = cl.StaticListRedirectPager(uris=uris)
selector = cl.CssSelector(pattern=".doulist-item")
analyzer = cl.AnalyzerPrettify(pager, selector)
res = []
for tr in analyzer(TestCase.limit):
print(tr)
res.append(tr)
print(len(res))
def test_03(self):
pager = cl.DynamicRedirectPager(uri="https://www.boc.cn/sourcedb/whpj/index.html",
uri_split="https://www.boc.cn/sourcedb/whpj/index_%v.html",
start=0)
selector = cl.CssSelector(
pattern="body > div > div.BOC_main > div.publish > div:nth-child(3) > table > tbody > tr")
analyzer = cl.AnalyzerPrettify(pager, selector)
res = []
for tr in analyzer(TestCase.limit):
print(tr)
res.append(tr)
print(len(res))
pager.webdriver.quit()
def test_04(self):
uris = ["https://www.boc.cn/sourcedb/whpj/index.html",
"https://www.boc.cn/sourcedb/whpj/index_1.html",
"https://www.boc.cn/sourcedb/whpj/index_2.html",
"https://www.boc.cn/sourcedb/whpj/index_3.html",
"https://www.boc.cn/sourcedb/whpj/index_4.html"]
pager = cl.DynamicListRedirectPager(uris=uris)
selector = cl.CssSelector(
pattern="body > div > div.BOC_main > div.publish > div:nth-child(3) > table > tbody > tr")
analyzer = cl.AnalyzerPrettify(pager, selector)
res = []
for tr in analyzer(TestCase.limit):
print(tr)
res.append(tr)
print(len(res))
pager.webdriver.quit()
def test_05(self):
pager = cl.DynamicScrollPager(uri="https://ec.ltn.com.tw/list/international")
selector = cl.CssSelector(pattern="#ec > div.content > section > div.whitecon.boxTitle.boxText > ul > li")
analyzer = cl.AnalyzerLinks(pager=pager, selector=selector)
res = []
for tr in analyzer(100):
print(tr)
res.append(tr)
print(len(res))
pager.webdriver.quit()
def test_06(self):
button_selector = cl.CssWebElementSelector(pattern='#newslist > button')
pager = cl.DynamicLineButtonPager(button_selector=button_selector)
pager.webdriver.get("https://www.yicai.com/news/kechuang/")
selector = cl.XpathSelector(pattern='//*[@id="newslist"]/a')
analyzer = cl.AnalyzerPrettify(pager=pager, selector=selector)
res = []
for tr in analyzer(TestCase.limit):
print(tr)
res.append(tr)
print(len(res))
pager.webdriver.quit()
def test_07(self):
button_selector = cl.CssWebElementSelector(pattern='#kkpager > div:nth-child(1) > span.pageBtnWrap > a')
pager = cl.DynamicNumButtonPager(uri="https://pccz.court.gov.cn/pcajxxw/gkaj/gkaj",
button_selector=button_selector)
selector = cl.CssSelector(pattern='#gkajlb ul')
analyzer = cl.AnalyzerPrettify(pager=pager, selector=selector)
res = []
for tr in analyzer(TestCase.limit):
print(tr)
res.append(tr)
print(len(res))
pager.webdriver.quit()
def test_08(self):
button_selector = cl.CssWebElementSelector(pattern='#downpage')
pager = cl.DynamicNextButtonPager(
button_selector=button_selector, start=2, offset=2)
pager.webdriver.get("https://news.lnd.com.cn/xwyw/index.shtml")
selector = cl.CssSelector(
pattern='body > div:nth-child(5) > div:nth-child(2) > div:nth-child(1) > div.main_more > span > ul > li')
analyzer = cl.AnalyzerPrettify(pager=pager, selector=selector)
res = []
for tr in analyzer(TestCase.limit):
print(tr)
res.append(tr)
print(len(res))
pager.webdriver.quit()
def test_09(self):
class MyPager(cl.DynamicLineButtonPager):
def pre_load(self, webdriver: WebDriver) -> None:
webdriver.get("https://kuaixun.eastmoney.com/")
cl.Action.click(webdriver,
'/html/body/div[1]/div[1]/div[2]/div[1]/div[1]/div[1]/div[1]/div[3]/label/span[1]')
pager = MyPager(uri="https://kuaixun.eastmoney.com/",
button_selector=cl.CssWebElementSelector('#news_list > div.load_more'))
selector = cl.CssSelector(pattern="#news_item_collection > div")
analyzer = cl.AnalyzerPrettify(pager, selector)
res = []
for tr in analyzer(TestCase.limit):
print(tr)
res.append(tr)
print(len(res))
def test_10(self):
button_selector = cl.CssWebElementSelector(pattern='#frs_list_pager > a')
uri = "https://tieba.baidu.com/f?kw=%E8%B4%B4%E5%90%A7&ie=utf-8&pn=0"
class MyPager(cl.DynamicNumButtonPager):
def pre_load(self, webdriver: WebDriver) -> None:
webdriver.get(uri)
# You also could use the selenium methods.
element = webdriver.find_element(By.XPATH, '//*[@id="tiebaCustomPassLogin"]/div[2]/span')
element.click()
pager = MyPager(uri=uri,
button_selector=button_selector)
selector = cl.CssSelector(pattern='#thread_list > li')
analyzer = cl.AnalyzerPrettify(pager=pager, selector=selector)
res = []
for tr in analyzer(TestCase.limit):
print(tr)
res.append(tr)
print(len(res))
pager.webdriver.quit()
def test_11(self):
class MyRequest(cl.Request):
def __init__(self):
pass
def request(self, uri: str) -> str:
try:
headers = {
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36",
'Cache-Control': 'max-age=0',
}
r = requests.get(uri, headers=headers)
r.raise_for_status()
r.encoding = r.apparent_encoding
return r.text
except HTTPError:
return ""
pager = cl.StaticRedirectPager(
uri="https://www.douban.com/doulist/893264/?start=0&sort=seq&playable=0&sub_type=",
uri_split="https://www.douban.com/doulist/893264/?start=%v&sort=seq&playable=0&sub_type=",
start=0,
offset=25, request=MyRequest())
selector = cl.CssSelector(pattern=".doulist-item")
analyzer = cl.AnalyzerPrettify(pager, selector)
res = []
for tr in analyzer(TestCase.limit):
print(tr)
res.append(tr)
print(len(res))
def test_12(self):
class MyDriver(cl.Driver):
def __init__(self):
pass
def get_driver(self) -> WebDriver:
option = wd.ChromeOptions()
option.add_argument("start-maximized")
option.add_argument("--headless")
option.add_argument("window-size=1920x3000")
agent = 'user-agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36"'
option.add_argument(agent)
my_webdriver = wd.Chrome(service=Service(ChromeDriverManager().install()), options=option)
return my_webdriver
pager = cl.DynamicScrollPager(uri="https://ec.ltn.com.tw/list/international", webdriver=MyDriver())
selector = cl.CssSelector(pattern="#ec > div.content > section > div.whitecon.boxTitle.boxText > ul > li")
analyzer = cl.AnalyzerPrettify(pager=pager, selector=selector)
res = []
for tr in analyzer(TestCase.limit):
print(tr)
res.append(tr)
print(len(res))
pager.webdriver.quit()
def test_13(self):
pager = cl.DynamicScrollPager(uri="https://www.qtvnews.com/lanjing/qd/view/listData.html?id=10390", interval=3)
selector = cl.CssSelector(pattern='#datalist > dl')
analyzer = cl.AnalyzerPrettify(pager=pager, selector=selector)
res = []
for tr in analyzer(TestCase.limit):
print(tr)
res.append(tr)
print(len(res))
pager.webdriver.quit()
def test_14(self):
class MyPager(cl.DynamicNumButtonPager):
def pre_load(self, webdriver: WebDriver) -> None:
script = {
"method": "redirect",
"url": "https://www.baidu.com/",
"next": {
"method": "inputKeyword",
"xpath": "//*[@id=\"kw\"]",
"keyword": "和泉雾纱",
"next": {
"method": "click",
"xpath": "//*[@id=\"su\"]"
}
}
}
cl.Script(script)(webdriver)
pager = MyPager(uri="https://www.baidu.com/",
button_selector=cl.XpathWebElementSelector('//*[@id="page"]/div/a/span'),
webdriver=cl.DefaultDriver(is_debug=True), interval=5)
selector = cl.XpathSelector(pattern='/html/body/div[3]/div[3]/div[1]/div[3]/div')
analyzer = cl.AnalyzerPrettify(pager, selector)
res = []
for tr in analyzer(TestCase.limit):
print(tr)
res.append(tr)
print(len(res))
pager.webdriver.quit()
if __name__ == '__main__':
unittest.main()