-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.py
More file actions
74 lines (65 loc) · 1.87 KB
/
Copy pathmain.py
File metadata and controls
74 lines (65 loc) · 1.87 KB
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
import os
import json
import time
from selenium import webdriver
from time import sleep
import random
import datetime
home_url = 'http://t66y.com/thread0806.php?fid=7'
dr = webdriver.Chrome()
dr.set_page_load_timeout(120)
dr.get(home_url)
print("请先手动登录")
os.system('pause')
print("开始自动回复")
history = {}
def format_date():
return time.strftime("%Y-%m-%d %H:%M:%S")
def randomReply():
f = open('reply.json', 'r', encoding='utf-8')
replies = json.loads(f.read())
return replies[random.randint(0, len(replies) - 1)]
def reply():
tr3s = dr.find_elements_by_css_selector(".tr3.t_one.tac")
tr3 = tr3s[-1] # 最后一行
tds = tr3.find_elements_by_xpath("./*")
a = tds[1].find_element_by_tag_name("a")
href = a.get_attribute("href") # 获取链接
title = a.get_attribute("text") # 标题
print(format_date(), "进入帖子", title)
dr.get(href)
sleep(random.randint(10, 30))
if href in history:
print("此贴已回复过")
return
textarea = dr.find_element_by_name("atc_content") # 输入框
s = randomReply()
textarea.send_keys(s)
btn = dr.find_element_by_name("Submit")
btn.click()
print(format_date(), "回复", s)
history[href] = True
return s
start_hour = 9
today_count = 0
while True:
try:
dr.get(home_url)
print("进入技术区")
sleep(random.randint(5, 10))
now = datetime.datetime.now()
if now.hour < start_hour:
today_count = 0
print(format_date(), "未到时间")
elif today_count < 10:
s = reply()
if s:
today_count = today_count + 1
else:
print(format_date(), "今天的回帖上限已到")
sleep(1024+random.randint(10, 1024)) # 1024秒后随机回复一次
except Exception as e:
print(e)
print("加载超时,5分钟后重试")
sleep(5*60)
# dr.close()