from
tkinter
import
Tk, Label, Entry, Button
import
time
from
threading
import
Thread
import
pygame
import
selenium
from
selenium.common.exceptions
import
NoSuchElementException
from
selenium.webdriver.support.ui
import
WebDriverWait
from
selenium.webdriver.support
import
expected_conditions as ec
from
selenium.common.exceptions
import
TimeoutException
from
selenium
import
webdriver
from
selenium.webdriver.chrome.options
import
Options
from
selenium.webdriver.common.by
import
By
chrome_options
=
Options()
chrome_options.add_argument(
"--user-data-dir=C:\\Users\\Administrator\\AppData\\Local\\Google\\Chrome\\User Data\\Default"
)
driver
=
webdriver.Chrome(options
=
chrome_options)
def
wait_for_inputs():
while
not
entry1.get()
or
not
entry2.get():
root.update_idletasks()
def
wait_for_element(driver0, xpath, timeout
=
10
):
try
:
element
=
WebDriverWait(driver, timeout).until(
ec.presence_of_element_located((By.XPATH, xpath))
)
return
element
except
TimeoutException:
return
None
def
open_chrome():
wait_for_inputs()
driver.get(
"https://www.douyin.com/user/self?showTab=post"
)
def
run_code():
current_cycle
=
0
for
_
in
range
(
int
(entry2.get())):
current_cycle
+
=
1
root.title(f
"抖音监控私信 - 第 {current_cycle} 次循环"
)
if
current_cycle
=
=
int
(entry2.get()):
root.title(
"当前循环结束"
)
try
:
element
=
wait_for_element(driver,
'//*[@id="island_b69f5"]/div/ul[2]/div/li/div/div/div[2]/span/span'
)
if
element:
value
=
element.text
print
(
"元素的值:"
, value)
print
(
"出现新的信息,开始执行警报"
)
pygame.mixer.init()
sound
=
pygame.mixer.Sound(
"D:/mp3/XX.wav"
)
sound.play()
pygame.time.wait(
2000
)
pygame.mixer.music.stop()
tk_label.config(text
=
"注意有新的信息出现,请及时查看"
)
tk_label.after(
10000
,
lambda
: tk_label.config(text
=
""))
action
=
webdriver.ActionChains(driver)
action.move_to_element(element).click().perform()
element
=
wait_for_element(driver,
'//*[@id="island_b69f5"]/div/ul[2]/div/li/div/div/div[3]/div/div/div['
'1]/div/div[2]/div/div[1]/div/div/div[3]/span/span'
)
action
=
webdriver.ActionChains(driver)
action.move_to_element(element).click().perform()
text_block
=
driver.find_element(By.CLASS_NAME,
"public-DraftStyleDefault-block"
)
text_block.send_keys(
"你好,我这边是顺翔云仓,如果没有回复您可以直接电联,详看个人资料"
)
element0
=
driver.find_element(By.CLASS_NAME,
"sCp7KhBv.e2e-send-msg-btn"
)
print
(
"准备进行点击发送按钮"
)
element0.click()
element2
=
driver.find_element(By.CLASS_NAME,
"F2qAlnLO"
)
element2.click()
time.sleep(
int
(entry1.get()))
else
:
print
(
"暂时没有新的私信数量消息,开始执行查找是否有新的陌生人消息"
)
element
=
wait_for_element(driver,
"//p[contains(text(), '私信')]"
)
if
element:
print
(
"找到私信图片,鼠标即将移动点击"
)
action
=
webdriver.ActionChains(driver)
action.move_to_element(element).click().perform()
time.sleep(
2
)
element
=
driver.find_element(By.CLASS_NAME,
"TQyd2bgK"
)
if
element:
print
(
"找到陌生人图标,开始点击"
)
action
=
webdriver.ActionChains(driver)
action.move_to_element(element).click().perform()
time.sleep(
5
)
element
=
driver.find_element(By.CLASS_NAME,
"tJjNB1rt.ge6Vyp_V"
)
if
element:
print
(
"出现新的陌生人信息,开始执行警报"
)
pygame.mixer.init()
sound
=
pygame.mixer.Sound(
"D:/mp3/XX.wav"
)
sound.play()
pygame.time.wait(
2000
)
pygame.mixer.music.stop()
tk_label.config(text
=
"注意有新的陌生人信息出现,请及时查看"
)
tk_label.after(
10000
,
lambda
: tk_label.config(text
=
""))
print
(
"开始点击陌生人消息"
)
action
=
webdriver.ActionChains(driver)
action.move_to_element(element).click().perform()
text_block
=
driver.find_element(By.CLASS_NAME,
"public-DraftStyleDefault-block"
)
text_block.send_keys(
"你好,我这边是顺翔云仓,如果没有回复您可以直接电联,详看个人资料"
)
element0
=
driver.find_element(By.CLASS_NAME,
"sCp7KhBv.e2e-send-msg-btn"
)
print
(
"准备进行点击发送按钮"
)
element0.click()
print
(
"准备点击回关钮"
)
element
=
driver.find_element(By.CLASS_NAME,
"SHV6n6VV"
)
action
=
webdriver.ActionChains(driver)
action.move_to_element(element).click().perform()
time.sleep(
5
)
print
(
"操作完成,开始返回到正常聊天列表"
)
element
=
driver.find_element(By.CLASS_NAME,
'JkInzlFQ'
)
print
(
"开始点击返回按钮"
)
action
=
webdriver.ActionChains(driver)
action.move_to_element(element).click().perform()
else
:
print
(
"暂时没有陌生人的消息,跳过"
)
tk_label.config(text
=
"暂时没有陌生人的消息,跳过"
)
tk_label.after(
5000
,
lambda
: tk_label.config(text
=
""))
except
NoSuchElementException as e:
print
(f
"找不到元素:{e}"
)
print
(
"目前没有陌生人的消息,此项查询跳过"
)
tk_label.config(text
=
"目前没有陌生人的消息,此项查询跳过"
)
tk_label.after(
5000
,
lambda
: tk_label.config(text
=
""))
except
TimeoutException:
print
(
"元素超时未找到,跳过当前循环"
)
continue
except
selenium.common.exceptions.StaleElementReferenceException:
print
(
"遇到过时元素引用异常,跳过并重新开始"
)
continue
def
thread_runner():
Thread(target
=
run_code).start()
root
=
Tk()
root.title(
"抖音监控私信"
)
root.geometry(
"250x110"
)
tk_label
=
Label(root, text
=
"结果展示"
, width
=
30
, bd
=
10
, bg
=
"yellow"
)
tk_label.pack(pady
=
3
)
label1
=
Label(root, text
=
"间隔时间:"
)
label1.pack()
label1.place(x
=
0
, y
=
50
)
entry1
=
Entry(root, width
=
8
)
entry1.pack()
entry1.place(x
=
60
, y
=
50
)
label2
=
Label(root, text
=
"循环次数:"
)
label2.pack()
label2.place(x
=
0
, y
=
80
)
entry2
=
Entry(root, width
=
8
)
entry2.pack()
entry2.place(x
=
60
, y
=
80
)
def
set_buttons():
button2
=
Button(root, text
=
"登录"
, width
=
5
, command
=
open_chrome)
button2.pack(pady
=
10
)
button2.place(x
=
135
, y
=
60
)
button
=
Button(root, text
=
"运行"
, width
=
5
, command
=
thread_runner)
button.pack(pady
=
10
)
button.place(x
=
195
, y
=
60
)
set_buttons()
root.mainloop()