Python 爬虫监控女神的QQ空间新的说说,实现秒赞,并发送说说内容到你的邮箱
生活随笔
收集整理的這篇文章主要介紹了
Python 爬虫监控女神的QQ空间新的说说,实现秒赞,并发送说说内容到你的邮箱
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
這個文章主要是在前一篇文章上新增了說說秒贊的功能
前一篇文章可以了解一下
那么,這次主要功能就是
監控女神的 QQ空間,一旦女神發布新的說說,馬上點贊,你的郵箱馬上就會收到說說內容,是不是想了解一下
直接上代碼吧:
# -*- coding:utf-8 -*- from selenium import webdriver import time import osfrom email.mime.text import MIMEText import smtplib# by 留歌36#注意配置你的原始郵箱(發送郵件的郵箱) account = "###" password = "###"#注意配置你的接收郵箱 to = "###"#注意配置你女神的郵箱 qq="###"#使用Selenium的webdriver實例化一個瀏覽器對象,在這里使用Phantomjs driver = webdriver.PhantomJS(executable_path=r"F:\\phantomjs.exe")# 登錄QQ空間 def get_shuoshuo(qq):try:#注意配置你的女神的QQ號碼http://user.qzone.qq.com/{你的女神的qq號碼}/311'.format(qq)driver.get('http://user.qzone.qq.com/你的女神的qq號碼/311'.format(qq))time.sleep(5)driver.find_element_by_id('login_div')except:print("#####,沒有找到login_div,說明您在線########")sea()time.sleep(10)#注意配置你女神的qq號碼get_shuoshuo('###')else:print("#####,找到login_div,說明您沒有登陸,現在開始登陸########")# 如果頁面存在登錄的DIV,則模擬登錄driver.switch_to.frame('login_frame')driver.find_element_by_id('switcher_plogin').click()driver.find_element_by_id('u').clear() # 選擇用戶名框#注意配置你的qq賬號driver.find_element_by_id('u').send_keys('###')driver.find_element_by_id('p').clear()#注意配置你的qq密碼driver.find_element_by_id('p').send_keys('###')driver.find_element_by_id('login_button').click()time.sleep(3)print("#####登陸成功########")#注意配置你女神的qq賬號get_shuoshuo('###')#判斷好友空間是否設置了權限,通過判斷是否存在元素ID:QM_OwnerInfo_Icondriver.implicitly_wait(3)def sea():try:driver.find_element_by_id('QM_OwnerInfo_Icon')b = Trueexcept:b = False#如果有權限能夠訪問到說說頁面,那么定位元素和數據,并解析if b == True:driver.switch_to.frame('app_canvas_frame')content = driver.find_elements_by_css_selector('.content')stime = driver.find_elements_by_css_selector('.c_tx.c_tx3.goDetail')btns = driver.find_elements_by_css_selector('.qz_like_btn.c_tx.mr8')shuoshuo = content[0].textshijian= stime[0].textcontext = '說說內容為:'+ shuoshuo# context = '說說內容為:' + shuoshuo + " " + '發表時間是:' + shijiancun = ''.join(context)if toCheck(cun):# 調用點贊的函數print("##########準備點贊##########")dian(btns)print("##########點贊完成##########")# 調用發送郵件的函數print("##########準備發送郵件##########")send(to, "女神空間更新啦~~", context)print("##########發送郵件完成##########")print("##########準備保存說說##########")toSave(cun)print("##########保存說說完成##########")else:print('pass,還沒有更新新的說說')def dian(btns):n = 0;for btn in btns:try:if btn.is_displayed() == True:if btn.text.find(u"贊") == 0:btn.click()n += 1print ("點贊", n)except:print("已經點贊過了第一條說說")passdef send(to, title, content):msg = MIMEText(content)msg['Content-Type'] = 'text/plain; charset="utf-8"'msg['Subject'] = titlemsg['From'] = accountmsg['To'] = totry:smtp = smtplib.SMTP()smtp.connect('smtp.163.com')smtp.login(account, password)smtp.sendmail(account, to, msg.as_string())smtp.close()except Exception as e :print (str(e))def toCheck(data):if not os.path.exists('lhshuoshuo.txt'):return Trueelse:f = open('lhshuoshuo.txt','r')existshuoshuo= f.readlines();if data + '\n' in existshuoshuo:return Falseelse:return Truedef toSave(text):f = open('lhshuoshuo.txt' , mode='a')f.write(text+ '\n')f.close()#讓代碼在服務器上一直跑著 while True:get_shuoshuo(qq)*還是那樣,如果你有什么好的建議可以留言呢
有什么問題也可以留言,我會盡量第一時間回復你的*
總結
以上是生活随笔為你收集整理的Python 爬虫监控女神的QQ空间新的说说,实现秒赞,并发送说说内容到你的邮箱的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python数据分布统计_Python
- 下一篇: 从0到1亿美元 ---- PopCap创