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
def run(self):
print ('Current URL:', self.browser.current_url)
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.get(self.url)
time.sleep(5)
self.browser.switch_to.frame(0)
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 ("登录成功")
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")
self.start_praising()
self.log_num = len(self.log_list)
self.log_list=[]
print(22)
print(33)
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()
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)
print(3)
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]")
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()