利用阿里雲的函數計算實現百度貼吧免費定時簽到

解放雙手的時間到了。

先說下什麼是函數計算

函數計算(Function Compute)是一個事件驅動的全託管 Serverless 計算服務。您無需管理服務器等基礎設施,只需編寫代碼並上傳。函數計算會爲您準備好計算資源,並以彈性、可靠的方式運行您的代碼。

1,點擊進入函數計算入口

進入函數計算控制檯

2,創建服務函數並執行

 

創建事件函數

填寫函數運行環境(服務名字唯一,提示已經存在請改名)

編寫函數

# -*- coding: utf8 -*-
from requests import Session
from time import sleep

def handler(*args):
    # 數據
    like_url = 'https://tieba.baidu.com/mo/q/newmoindex?'
    sign_url = 'http://tieba.baidu.com/sign/add'
    tbs = '4fb45fea4498360d1547435295'
    head = {
        'Accept': 'text/html, */*; q=0.01',
        'Accept-Encoding': 'gzip, deflate',
        'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
        'Connection': 'keep-alive',
        'Cookie': '(這裏是自己的百度cookie,獲取方法自己百度)',
        'Host': 'tieba.baidu.com',
        'Referer': 'http://tieba.baidu.com/i/i/forum',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
                      'Chrome/71.0.3578.98 Safari/537.36',
        'X-Requested-With': 'XMLHttpRequest'}
    s = Session()

    # 獲取關注的貼吧
    bars = []
    dic = s.get(like_url, headers=head).json()['data']['like_forum']
    for bar_info in dic:
        bars.append(bar_info['forum_name'])

    # 簽到
    already_signed_code = 1101
    success_code = 0
    need_verify_code = 2150040
    already_signed = 0
    succees = 0
    failed_bar = []
    n = 0

    while n < len(bars):
        sleep(0.5)
        bar = bars[n]
        data = {
            'ie': 'utf-8',
            'kw': bar,
            'tbs': tbs
        }
        try:
            r = s.post(sign_url, data=data, headers=head)
        except Exception as e:
            print(f'未能簽到{bar}, 由於{e}。')
            failed_bar.append(bar)
            continue
        dic = r.json()
        msg = dic['no']
        if msg == already_signed_code:
            already_signed += 1; r = '已經簽到過了!'
        elif msg == need_verify_code:
            n -= 1; r = '需要驗證碼,即將重試!'
        elif msg == success_code:
            r = f"簽到成功!你是第{dic['data']['uinfo']['user_sign_rank']}個簽到的吧友,共簽到{dic['data']['uinfo']['total_sign_num']}天。"
        else:
            r = '未知錯誤!' + dic['error']
        print(f"{bar}:{r}")
        succees += 1
        n += 1
    l = len(bars)
    failed = "\n失敗列表:" + '\n'.join(failed_bar) if len(failed_bar) else ''
    print(f'''共{l}個吧,其中: {succees}個吧簽到成功,{len(failed_bar)}個吧簽到失敗,{already_signed}個吧已經簽到。{failed}''')

運行上面代碼

查看執行日誌

3,創建觸發器(每天凌晨一點執行)

附:cron表達式在線生成器

4,收費情況

有兩種付費方式一種按量收費,一種包年包月,詳細情況請閱讀文檔

可以放心的是每個月有免費額度

到此完成,靜等每天簽到即可。

然而云函數的用法不止如此,感興趣的小夥伴可以去看官方文檔以及最佳實踐,有疑問的小夥伴可以留言或者添加左側微信。

附:最佳實踐

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章