Python-based simple comment area lottery

foreword

blogger empty star
home page empty star's homepage

Hello everyone, I am Kongkong star, and I will share this article with you 《基于Python的简易评论区抽奖》.

import module

import random
import requests

Get Blog Comments

It should be enough to set the size to 1000, if it is not enough, adjust it according to the actual situation.

def comment_list(username,article_id):
    url = f'https://blog.csdn.net/phoenix/web/v1/comment/list/{
      
      article_id}?page=1&size=1000'
    headers = {
    
    
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/18.17763',
        'Cookie': f'UserName={
      
      username}'
    }
    res = requests.get(url, headers=headers)
    comments = res.json()['data']['list']
    comment_dicts = []
    for comment in comments:
        if comment['info']['parentId'] == 0:
            content = comment['info']['content']
            userName = comment['info']['userName']
            nickName = comment['info']['nickName']
            comment_dict = {
    
    
                'userName': userName,
                'nickName': nickName,
                'content': content
            }
            comment_dicts.append(comment_dict)
    return comment_dicts

Extract users

There are several points to consider:

  • exclude the author himself
  • Have you commented as requested
  • Extracted user duplicates
  • The number of users to be extracted is less than the required number (of course, under normal circumstances, 3 or 5 are enough)
def select_users(username,base_content,comment_ds,num):
    users = []
    if base_content is None:
        for item in comment_ds:
            users.append(item['userName'])
    else:
        for item in comment_ds:
            # 筛选出按照要求评论的用户
            if item['content'] == base_content:
                users.append(item['userName'])
    # 移除作者自己
    if username in users:
        users.remove(username)
    if num > len(set(users)):
        print('待抽取用户个数不足抽取的个数!')
    else:
        selected_users = random.sample(users, num)
        if len(selected_users) != len(set(selected_users)):
            print('存在重复用户,请重新抽取!')
        else:
            print(f'中奖用户:{
      
      selected_users}')

program entry

Here is a demonstration of this blog by Boss Chen:

if __name__ == '__main__':
    # 你的username
    username = 'weixin_47343544'
    # 参与抽奖活动的博客id
    article_id = 131788124
    # 你要求的评论内容
    base_content = '学java就找陈老老老板'
    # base_content = None
    # 抽取人数
    num = 3
    comment_ds = comment_list(username, article_id)
    select_users(username, base_content, comment_ds, num)

Effect

哈哈,竟然抽到了自己。

Winning users: ['m0_64280701', 'H1727548', 'weixin_38093452']
Process finished with exit code 0

Summarize

at last

如果您不知道如何支持我,
InsCode AI列了一些支持博主的句子供您参考:

The article written by the blogger is very in-depth and has gained a lot of knowledge.
The blogger has a humorous and witty writing style.
Bloggers have the courage to share their experiences and lessons, which benefit beginners a lot.
The blogger's thoughts are unique, and the articles are particularly exciting to read.
The blogger is very nice, helpful, and responds to readers' questions in a timely manner.
The blogger's professional knowledge is very comprehensive, and he can give detailed answers to questions in any field.

Guess you like

Origin blog.csdn.net/weixin_38093452/article/details/131855680