抽奖来试试这个py吧,排除楼主和重复【Python】

看到LINUX DO经常有热佬抽奖,好像也有插件什么的,我也想着怎么抽奖?
应该要排除楼主,还要排除重复的回帖吧,最后随机一个或多个中奖者。展示出中奖者的信息,经过摸索用我小白的Python糊了一个。

没有解决的问题:就是带锁的帖子请求不到数据(貌似大部分都是带锁),我也不太清楚要哪些鉴权方式(试过cookie貌似不行),代理访问也没做,可能还会遇到其他问题,大家看看怎么请求带锁的帖子?
原来是我cookie没放全,我也不知道哪些是必须,我就都放上了,好像可以了,大家以后抽奖可以试试 :grinning:

运行效果:

复制文本到帖子中的样子:

Python代码
import requests
import random


def get_candidate_list(url):
    headers = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36",
                "Referer":"https://linux.do",
                "Cookie":"把你的cookie放在这,对全部的"}
    resp = requests.get(url, headers=headers,timeout=10)
    if resp.status_code == 403:
        print("【带锁的帖子】没有权限查看请求的资源。,我也不会了")
        exit()
        return None
    result = resp.json()
    posts = result["post_stream"]["posts"]
    topic_owner = posts[0]["username"]
    candidate_list = []
    name_list = []
    for i in posts:         
        only_name = i["username"]
        if (only_name not in name_list) and (only_name != topic_owner): # 去除楼主和重复username
            candidate = {
            "only_name": only_name,
            "display_name": i["display_username"],
            "post_text": i["cooked"],
            "post_number": i["post_number"]
            }
            candidate_list.append(candidate)
        name_list.append(i["username"])
    return candidate_list


def start(url,winner_number):
    url = url.rsplit('/', 1)[0]
    candidates = get_candidate_list(url+'.json')
    print(f"\n主题共 {len(candidates)} 个有效回复参与抽奖(不包含楼主和重复回帖):\n【{winner_number}】位中奖者信息如下:\n")
    for x in (random.sample(candidates, winner_number)):
        winner_id = x["only_name"]
        winner_name = x["display_name"]
        winner_text = x["post_text"]
        winner_floor = x["post_number"]
        print(f"# 恭喜 {winner_name}(@{winner_id}),楼层 [{winner_floor}]({url}/{winner_floor})  \n回帖内容:\n> {winner_text}\n")

    print("\n复制以上内容发帖即可。")

if __name__ == "__main__":
    url = 'https://linux.do/t/topic/112343/1' # 抽奖页面网址
    winner_number = 3  # 中奖人数
    start(url,winner_number)
6 个赞

好好好,设置一下 cookie 就行了

4 个赞

确实是cookie没放全

:cow: :cow: :cow:

这个好诶

那既然 都架到这了 不抽点什么试试吗 :face_holding_back_tears:

:grinning:等到20个回复,那我就到时候来抽5个点赞吧 :grinning:

1 个赞

搞七捻三软件分享

抽我 哈哈

mark