Python 爬蟲監控女神的QQ空間新的說說,實現秒贊,併發送說說內容到你的郵箱

這個文章主要是在前一篇文章上新增了說說秒讚的功能

前一篇文章能夠瞭解一下css

那麼,此次主要功能就是web

監控女神的 QQ空間,一旦女神發佈新的說說,立刻點贊,你的郵箱立刻就會收到說說內容,是否是想了解一下

直接上代碼吧:canvas

# -*- coding:utf-8 -*-
from selenium import webdriver
import time
import os

from 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_Icon
    driver.implicitly_wait(3)

def sea():
    try:
        driver.find_element_by_id('QM_OwnerInfo_Icon')
        b = True
    except:
        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].text
        shijian= stime[0].text
        context = '說說內容爲:'+ shuoshuo
        # context = '說說內容爲:' + shuoshuo + "       " + '發表時間是:' + shijian
        cun = ''.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 += 1
                    print ("點贊", n)

        except:
            print("已經點贊過了第一條說說")
            pass

def send(to, title, content):
    msg = MIMEText(content)
    msg['Content-Type'] = 'text/plain; charset="utf-8"'
    msg['Subject'] = title
    msg['From'] = account
    msg['To'] = to
    try:
        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 True
    else:
        f = open('lhshuoshuo.txt','r')
        existshuoshuo= f.readlines();
        if data + '\n' in existshuoshuo:
            return False
        else:
            return True

def toSave(text):
    f = open('lhshuoshuo.txt' , mode='a')
    f.write(text+ '\n')
    f.close()

#讓代碼在服務器上一直跑着
while True:
    get_shuoshuo(qq)

仍是那樣,若是你有什麼好的建議能夠留言呢
有什麼問題也能夠留言,我會盡可能第一時間回覆你的
瀏覽器

相關文章
相關標籤/搜索