python+selenium自动给指定的人留言天气状况

最近天气忽冷忽热,提醒你最关心的人天气状况
import datetime,time
from selenium import webdriver
import requests
import json,os

def auto_msg(info):
    appkey = "094cd596bdc040b0b50eab514e******"  #从图灵得到的授权码
    url = "http://www.tuling123.com/openapi/api?key=%s&info=%s"%(appkey,info)
    content = requests.get(url).text
    data = json.loads(content)
    answer = data['text']
    return answer


tianqi = """[天气预报] """ + auto_msg('**天气')  #“‘’”里面的内容是固定内容,可以任意修改
print(tianqi)


def get_h():
    driver = webdriver.Chrome()
    driver.maximize_window()
    driver.get("http://user.qzone.qq.com/**********/334")#星号填写QQ号
    try:
        #driver.find_element_by_class_name('veIframeContainer editor_iframe_container_veditor1').send_keys('haha')
        time.sleep(3)
        driver.switch_to_frame('tgb')
        #iframe = driver.find_element_by_xpath("//*[@class='veIframeContainer editor_iframe_container_veditor1']/iframe")
        time.sleep(3)
        driver.switch_to.frame('veditor1_Iframe')
        driver.find_element_by_xpath('/html/body').send_keys(tianqi)
        time.sleep(5)
        driver.switch_to.parent_frame()
        driver.find_element_by_id('btnPostMsg').click()
    except:
    #driver.find_element_by_class_name("bg s_ipt_wr quickdelete-wrap")

        driver.switch_to_frame('login_frame')
        driver.find_element_by_id('switcher_plogin').click()
        driver.find_element_by_id('u').clear()
        driver.find_element_by_id('u').send_keys(os.environ.get('QQ_USER'))#或者填写你的QQ号
        time.sleep(1)
        driver.find_element_by_id('p').clear()
        driver.find_element_by_id('p').send_keys(os.environ.get('QQ_PASSWORD'))#把你的密码写进环境变量中,从环境变量中提取

        driver.find_element_by_id('login_button').click()
        time.sleep(3)       #这里最好休息3s以上,不然可能iframe界面刷新不出来
        driver.switch_to.frame('tgb')

        #iframe=driver.find_element_by_xpath("//*[@class='veIframeContainer editor_iframe_container_veditor1']/iframe")
        time.sleep(3)
        driver.switch_to.frame('veditor1_Iframe')
        driver.find_element_by_xpath('/html/body').send_keys(tianqi)
        time.sleep(5)
        driver.switch_to.parent_frame()
        print('fanhui')
        driver.find_element_by_id('btnPostMsg').click()
        time.sleep(5)
        print('wancheng')

a = 1
while a ==1:
    now = datetime.datetime.now()
    str = now.strftime('%Y-%m-%d %H:%M:%S')[11:]



    print('\r{}'.format(str),end='')
    time.sleep(1)
    if str=='22:06:43':    #这里填写你想定时发送的时间
        get_h()

你可能感兴趣的:(python)