-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathWebDriver.py
More file actions
161 lines (131 loc) · 6.34 KB
/
WebDriver.py
File metadata and controls
161 lines (131 loc) · 6.34 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
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
# -*- coding: utf-8 -*-
import re
import time
import urllib.request
from selenium import common, webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
USER_NAME = "把这里换成你的百度用户名"
PASSWORD = "把这里换成你的百度账号密码"
def login(username, password):
print("正在启动")
print("因为最近 Chrome 有大版本更新,如果出现错误请确认 ChromeDriver 已经升级到最新版本")
urlusername = urllib.request.quote(username)
driver.get("http://tieba.baidu.com/home/main?un=" + urlusername + "&fr=home")
driver.find_element_by_class_name("u_login").click()
time.sleep(2)
pageSouce = driver.page_source
elementidPrefix = re.findall(r"(TANGRAM__PSP_[0-9]{1,})__footerULoginBtn", pageSouce)[0]
driver.find_element_by_id(elementidPrefix + "__footerULoginBtn").click()
driver.find_element_by_id(elementidPrefix + "__userName").send_keys(username)
driver.find_element_by_id(elementidPrefix + "__password").send_keys(password)
driver.find_element_by_id(elementidPrefix + "__submit").click()
print("等待输入验证码,输入完成确定后请在此处按回车")
input()
print("正在运行...")
def my_tie_collector():
driver.get("http://tieba.baidu.com/i/i/my_tie")
listOfLinks = list()
listOfElements = driver.find_elements_by_class_name("thread_title")
for i in range(0, len(listOfElements)):
listOfLinks.append(listOfElements[i].get_attribute("href"))
driver.get("http://tieba.baidu.com/i/i/my_tie?&pn=2") #每天限制30贴,所以最多前两页就足够了
listOfElements = driver.find_elements_by_class_name("thread_title")
for i in range(0, len(listOfElements)):
listOfLinks.append(listOfElements[i].get_attribute("href"))
print("帖子链接收集完成")
return listOfLinks
def my_reply_collector():
driver.get("http://tieba.baidu.com/i/i/my_reply")
listOfLinks = list()
listOfElements = driver.find_elements_by_class_name("for_reply_context")
for i in range(0, len(listOfElements)):
listOfLinks.append(listOfElements[i].get_attribute("href"))
driver.get("http://tieba.baidu.com/i/i/my_reply?pn=2") #同上
listOfElements = driver.find_elements_by_class_name("for_reply_context")
for i in range(0, len(listOfElements)):
listOfLinks.append(listOfElements[i].get_attribute("href"))
print("帖子链接收集完成")
return listOfLinks
def deleter_tie(listOfLinks, username): #增加对楼中楼的删除功能
print("正在删除...")
for i in range(0, len(listOfLinks)):
try:
driver.get(listOfLinks[i])
time.sleep(1)
element = driver.find_element_by_class_name("p_post_del_my")
driver.execute_script("arguments[0].scrollIntoView(false);", element)
element.click()
time.sleep(0.3)
driver.find_element_by_class_name("dialogJanswers").find_element_by_tag_name("input").click()
print("删除成功")
continue
except common.exceptions.NoSuchElementException:
print("删除失败,尝试下一种搜寻方式")
try:
maincontent = driver.find_element_by_class_name("p_postlist")
elements = maincontent.find_elements_by_link_text(username)
for element in elements:
ActionChains(driver).move_to_element(element).perform()
try:
driver.find_element_by_link_text("删除").click()
break
except common.exceptions.NoSuchElementException:
print("Fail to find element, try next herf") #有可能别人@你导致选错元素,所以对每个超链接遍历一遍直到找到有删除按钮的
time.sleep(0.3)
driver.find_element_by_class_name("dialogJanswers").find_element_by_tag_name("input").click()
print("删除成功")
except common.exceptions.NoSuchElementException:
print("仍然删除,可能是以匿名发表")
def deleter_follows():
while True:
driver.get("http://tieba.baidu.com/i/i/concern")
try:
driver.find_element_by_class_name("btn_unfollow").click()
driver.find_element_by_class_name("dialogJbtn").click()
time.sleep(0.5)
except common.exceptions.NoSuchElementException:
print("关注已经删除完毕")
break
def deleter_fans():
while True:
driver.get("http://tieba.baidu.com/i/i/fans")
try:
element = driver.find_element_by_class_name("name")
ActionChains(driver).move_to_element(element).perform() #移动到名字否则取消关注不会出现
driver.find_element_by_id("add_blacklist_btn").click()
driver.find_element_by_class_name("dialogJbtn").click()
time.sleep(0.5)
except common.exceptions.NoSuchElementException:
print("粉丝已经删除完毕")
break
def deleter_BaIFollow(): #使用此功能需打开图片显示
driver.get("http://tieba.baidu.com/i/i/forum")
driver.find_element_by_class_name("pm_i_know").click()
while True:
try:
driver.find_element_by_class_name("pt").click()
driver.find_element_by_class_name("dialogJbtn").click()
time.sleep(0.5)
except common.exceptions.NoSuchElementException:
print("关注的贴吧已经删除完毕")
break
def Start_with_Chrome_without_images():
chrome_options = webdriver.ChromeOptions()
prefs = {
"profile.managed_default_content_settings.images": 2
}
chrome_options.add_experimental_option("prefs", prefs)
driver = webdriver.Chrome(chrome_options=chrome_options)
return driver
def Start_with_Chrome():
driver = webdriver.Chrome()
return driver
#driver = Start_with_Chrome_without_images() #不加载图片可以提高速度但是无法删除关注的贴吧
driver = Start_with_Chrome()
login(USER_NAME, PASSWORD)
deleter_tie(my_tie_collector(), USER_NAME)
deleter_fans()
deleter_follows()
deleter_BaIFollow()
print("全部完成")