-
Notifications
You must be signed in to change notification settings - Fork 121
/
Copy pathmain.py
62 lines (48 loc) · 1.46 KB
/
main.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
import json
import random
import time
import os
from selenium.webdriver.support.wait import WebDriverWait
from config import settings
from task import single_task
def main():
"""
程序主函数
:return: None
"""
driver = setup_driver()
wait = WebDriverWait(driver, settings['timeout'])
url_list = get_url_list()
count_result = {'success': 0, 'error': 0, 'sum': settings['times']}
for i in range(settings['times']):
url = url_list[random.randint(0, len(url_list) - 1)]
print(f'--------------TASK {i + 1}: {url["name"]} {url["url"]} --------------')
result = single_task(driver, url['url'], wait)
print(result)
count_result[result['status']] += 1
if result['status'] == 'success':
time.sleep(2)
print(f'\n{count_result}')
driver.close()
def setup_driver():
"""
初始化 driver
:return: driver
"""
driver = settings['driver']
driver.set_page_load_timeout(settings['timeout'])
driver.set_script_timeout(settings['timeout'])
driver.set_window_rect(0, 0, 1024, 768)
return driver
def get_url_list():
"""
随机选择一个 json 文件读入
:return: dict 数组,URL 列表
"""
file_list = os.listdir('assets')
filepath = os.path.join('assets', file_list[random.randint(0, len(file_list) - 1)])
with open(filepath) as f:
url_list = json.load(f)
return url_list
if __name__ == '__main__':
main()