-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy_selenium.py
103 lines (79 loc) · 3.65 KB
/
my_selenium.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
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
import time
import threading
import multiprocessing
def run_thread(keyword):
driver = webdriver.Chrome()
try:
driver.get("https://www.youtube.com/")
# Tìm ô tìm kiếm
search_box = driver.find_element(By.XPATH, '//input[@id="search"]')
# Gõ từ khóa vào ô tìm kiếm
search_box.send_keys("@Boymuscleworkout")
# Nhấn Enter để tìm kiếm
search_box.send_keys(Keys.RETURN)
time.sleep(5)
# Tìm và nhấp vào liên kết của kênh
video_link = driver.find_element(By.XPATH, '//*[@id="subscribers" and contains(text(), "@Boymuscleworkout")]')
video_link.click()
time.sleep(5)
playlists = driver.find_element(By.XPATH, '//yt-tab-shape[@tab-title="Playlists"]')
playlists.click()
time.sleep(5)
watch_video = driver.find_element(By.XPATH, '//a[@id="video-title" and contains(text(), "{}")]'.format(keyword))
watch_video.click()
time.sleep(10)
while True:
# Chụp ảnh màn hình
driver.save_screenshot("screenshot_{}_{}.png".format(keyword, time.time()))
print("Screenshot taken for keyword: {}".format(keyword))
# Chờ 10 phút trước khi chụp ảnh màn hình tiếp theo
time.sleep(300)
#Đóng trình duyệt khi kết thúc
driver.quit()
except:
driver.get("https://www.youtube.com/@Boymuscleworkout/playlists")
time.sleep(10)
watch_video = driver.find_element(By.XPATH, '//a[@id="video-title" and contains(text(), "{}")]'.format(keyword))
watch_video.click()
time.sleep(10)
while True:
# Chụp ảnh màn hình
driver.save_screenshot("screenshot_{}_{}.png".format(keyword, time.time()))
print("Screenshot taken for keyword: {}".format(keyword))
# Chờ 10 phút trước khi chụp ảnh màn hình tiếp theo
time.sleep(300)
# Đóng trình duyệt khi kết thúc
driver.quit()
# elements = ['kimnguyennanh', 'hmatuan9', 'rforrachman', 'mixed', 'tuangym98', 'huutinh103', 'bimostreetworkout', 'duongkimmochii', 'honguynvn04', 'buiquoc_sw', 'Hpp358965']
# elements = elements + elements
# # Số lượng luồng bạn muốn chạy
# n_threads = len(elements)
def run_process(elements):
# Tạo và khởi chạy các luồng trong mỗi process
threads = []
for element in elements:
thread = threading.Thread(target=run_thread, args=(element,))
thread.start()
threads.append(thread)
# Chờ tất cả các luồng hoàn thành trong mỗi process
for thread in threads:
thread.join()
if __name__ == "__main__":
# List các elements bạn muốn xử lý
elements = ["honguynvn04","hieuvilai2007"]
elements = elements + elements + elements + elements + elements + elements
# Số lượng process muốn tạo, có thể sử dụng multiprocessing.cpu_count() để lấy số lượng core CPU
num_processes = multiprocessing.cpu_count() - 2
# Tạo và khởi chạy các process
processes = []
for i in range(num_processes):
process = multiprocessing.Process(target=run_process, args=(elements,))
process.start()
processes.append(process)
# Chờ tất cả các process hoàn thành
for process in processes:
process.join()
print("Done!")