空间自动点赞

之前一直想要完成的一个小程序,在某种机缘巧合下基本抄了别人的代码,加了一点自己的东西,还是很满意的。


selenium模拟登陆点赞和刷新,代码比较的简单。
但是测试的时候真的什么情况都有
注意chromedriver 和 Chrome浏览器版本的对应

代码

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
from selenium import webdriver
import time
import traceback
class Praise():
def __init__(self,QQ,password,friendQQ):
self.QQ=QQ
self.password=password
self.friendQQ=friendQQ
self.n = 0
self.logs=""
self.praised_list = []
if self.friendQQ:
self.url="https://user.qzone.qq.com/"+self.friendQQ+"/main"
else:
self.url="https://user.qzone.qq.com/"+self.QQ
#print(self.url)
def run(self):
print ('Current URL:', self.browser.current_url)
# see more
try:
btn_more = self.browser.find_element_by_css_selector(".b-inline.data_btn_more")
if btn_more:
btn_more.click()
except:
pass
if self.friendQQ:
self.praise_someone()
else:
self.praise_all()
self.browser.quit()
def login_qzone(self):
self.browser = webdriver.Chrome()
#self.browser.maximize_window()
self.browser.get(self.url)
time.sleep(5)
self.browser.switch_to.frame(0)
#self.browser.switch_to.frame("login_frame")
print("hahaha")
self.browser.find_element_by_id("switcher_plogin").click()
print("in\n")
self.browser.find_element_by_id("u").clear()
self.browser.find_element_by_id("u").send_keys(self.QQ)
self.browser.find_element_by_id("p").clear()
self.browser.find_element_by_id("p").send_keys(self.password)
self.browser.find_element_by_id("login_button").click()
time.sleep(10)
print ("登录成功")
# 解决FireFox的登录成功后,直接访问新页面出现can't access dead object错误的方法链接:
# http://stackoverflow.com/questions/16396767/firefox-bug-with-selenium-cant-access-dead-object
# 通过下面这句解决,可能时因为上面switch_to到了login_frame,所以现在它是dead object
self.browser.switch_to.default_content()
def praise_someone(self):
self.log_num=0
pre_num=0
while 1:
self.browser.switch_to.frame("QM_Feeds_Iframe") # 个人主页才有
self.log_head = self.browser.find_element_by_id("host_home_feeds")
self.log_list=self.log_head.find_elements_by_css_selector(".f-single.f-s-s")
self.start_praising()
self.browser.switch_to.default_content()
for i in range(50):
self.browser.execute_script("window.scrollBy(0,300);")
time.sleep(2)
def praise_all(self):
feed_friend_list = self.browser.find_element_by_id("feed_friend_list")
self.log_list = feed_friend_list.find_elements_by_css_selector(".f-single.f-s-s")
# 向下翻动一段距离,为了使feed_page_container那个变化的列表出现
#time.sleep(60)
self.start_praising()
self.log_num = len(self.log_list)
self.log_list=[]
print(22)
#container_head = feed_friend_list.find_element_by_class_name("feed_page_container")
print(33)
# 个人中心页面,说说是在<li class="feed_page_container">...</li>下动态出现的
# 该标签下最多有3个<ul data-page="0">...</ul>,每个ul标签下又包含4个说说
# 这里面的数值12只是估算,因为说说的评论数不同,导致一条说说所占的高度不同
while 1:
time.sleep(2)
print("emmmmmmmmmmmmm")
feed_friend_list = self.browser.find_element_by_id("feed_friend_list")
self.log_list = feed_friend_list.find_elements_by_css_selector(".f-single.f-s-s")
print (len(self.log_list))
self.start_praising()
#time.sleep(5)
print("sleep test")
self.browser.refresh()
time.sleep(30)
for i in range(100):
self.browser.execute_script("window.scrollBy(0,-500);")
time.sleep(5)
print(1)
feed_friend_list = self.browser.find_element_by_id("feed_friend_list")
print(2)
#self.log_list = feed_friend_list.find_elements_by_css_selector(".f-single.f-s-s")
print(3)
#time.sleep(50)
#container_head = feed_friend_list.find_element_by_class_name("feed_page_container")
print(4)
def start_praising(self):
for log in self.log_list:
# 赞过的就不赞了
if log in self.praised_list:
continue
try:
self.n+=1
# 名字
print (self.n,"【",log.find_element_by_xpath("./div/div[4]/div/a").text,"】 :",)
# 说说内容
print (log.find_element_by_xpath("./div[2]/div/div").text,)
# 点赞图标
thumb_up_block=log.find_element_by_xpath("./div[3]/div[1]/p/a[3]")
# 若已赞过,则class属性中会增加一个CSS属性值item-on
if "item-on" not in thumb_up_block.get_attribute("class"):
thumb_up_icon=thumb_up_block.find_element_by_xpath("./i")
thumb_up_icon.click()
print ("[点赞成功]\n")
else:
print ("[已赞]\n")
self.praised_list.append(log)
time.sleep(1)
except:
traceback.print_exc()
continue
def main():
QQ =input(u"输入QQ号:")
password =input(u"输入QQ密码:")
friendQQ =input(u"输入被点赞的好友QQ号(不输入则给空间所有发出来的动态点赞):")
friendQQ = []
praise_spider = Praise(QQ,password,friendQQ)
praise_spider.login_qzone()
praise_spider.run()
if __name__ == "__main__":
main()

未解决的问题

文章目录
  1. 1. 代码
  2. 2. 未解决的问题
{{ live2d() }}