Python贴吧签到

 

本程序使用requests调用贴吧的签到接口,随机延迟2到5秒签到,

建议搭配定时构建食用,如:Jenkins、GitHub Action等,

以下是源码

import re
import time
import json
import random
import requests


headers = {
    "Referer": "https://tieba.baidu.com/",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64)",
    "Cookie": '把你的cookie复制到此处'
}

# 获取html信息
def getTiebaInfo():
    try:
        url = "https://tieba.baidu.com/"

        # 下载贴吧html页面
        response = requests.get(url=url, headers=headers)
        response.raise_for_status()
        response.encoding = response.apparent_encoding
        html = response.text

        # 提取tbs
        tbs = getTbs(html)

        # 提取关注列表
        forums = getAll(html)

        return {"forums": forums, "tbs": tbs}
    except Exception as e:
        print(f"获取数据失败:{e}")
        return None

# 获取关注的贴吧
def getAll(html):
    match = re.search(r'{"forums":\[.*\],"directory"', html)
    if match:
        data = match.group(0)
        forums = json.loads(data[data.find('['):data.rfind("]")+1])
        return forums
    return None

# 从html中提取tbs的值
def getTbs(html):
    match = re.search(r'PageData.tbs = "(.*)";PageData.is_iPad', html)
    if match:
        tbs = match.group(0).split('"')[1]
        return tbs
    return None

# 逐个签到
def tiebaSignin(tiebaInfo):
    # 签到接口
    signin_url = "https://tieba.baidu.com/sign/add"
    tbs = tiebaInfo.get("tbs")
    # 统计结果
    success_count = 0
    fail_count = 0
    # 签到
    for forum in tiebaInfo.get("forums"):
        # 跳过已经签到的贴吧,减少请求次数
        is_sign = forum.get("is_sign")
        if is_sign == 1:
            continue
        forum_name = forum.get("forum_name")
        sigin_data = {
            "ie": "utf-8",
            "kw": forum_name,
            "tbs": tbs
        }
        ti = time.strftime("%Y-%m-%d %H:%M:%S")
        try:
            # 发送请求签到
            response = requests.post(url=signin_url, headers=headers, data=sigin_data)
            response.raise_for_status()
            response.encoding = response.apparent_encoding
            content = response.json()
            # 判断签到结果,打印消息
            if content.get("no") == 0:
                success_count += 1
                print(f"{ti}   {forum_name}吧签到成功")
            else:
                fail_count += 1
                print(f"{ti}   {forum_name}吧签到失败,失败原因:{content.get("error")}")
        except Exception as e:
            fail_count += 1
            print(f"{ti}   Error: {forum_name}吧签到发生错误,{e}")
        # 随机睡眠1-5秒
        instant = random.randint(1, 5)
        time.sleep(instant)

    print(f"本次签到成功{success_count}个,失败{fail_count}个")

def main():
    print("-----------start-------------")
    tiebaInfo = getTiebaInfo()
    if tiebaInfo:
        tiebaSignin(tiebaInfo)
    else:
        print("签到失败")
    print("------------end--------------")

if __name__ == "__main__":
    main()

  • 6
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值