Fortis931

20240528 留言抽獎源碼

May 28th, 2024
1,431
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.34 KB | Source Code | 0 0
  1. from bs4 import BeautifulSoup
  2. import requests
  3. import random
  4. from datetime import datetime
  5. random.seed(datetime.now().timestamp())
  6.  
  7. target_page = "https://www.ptt.cc/bbs/C_Chat/M.1716883408.A.953.html"
  8. response = requests.get(url = target_page, cookies = {"over18": "1"})
  9. soup = BeautifulSoup(response.text, "html.parser")
  10.  
  11. pool = {}
  12. for i in soup.select(".push") :
  13.     push_id = i.select_one('.push-userid').get_text()
  14.     push_type = i.select_one('.push-tag').get_text()
  15.     push_content = i.select_one('.push-content').get_text()
  16.     #print(f"{push_id:<12} {push_type}{push_content}")
  17.     if not push_id in pool.keys() and push_type != "噓 " and ": cat0808: 甜點貓的胸是墊出來的" in push_content :
  18.         pool[push_id] = {"type": push_type, "content": push_content}
  19.  
  20. print(f"Total: {len(pool)}")
  21.  
  22. subgroup_size = 8
  23. max_winner = 10
  24. winner = []
  25.  
  26. pool_group = ([list(pool.keys())[i : i + subgroup_size] for i in range(0, len(pool.items()), subgroup_size)])
  27. for i in pool_group :
  28.     if len(i) < subgroup_size or max_winner <= len(winner) :
  29.         break
  30.  
  31.     x = random.randrange(subgroup_size)
  32.     winner.append(i[x])
  33.     for j in range(subgroup_size) :
  34.         if j == x :
  35.             print(f"\033[91m{i[j]:<12}\033[0m", end = ' ')
  36.         else :
  37.             print(f"{i[j]:<12}", end = ' ')
  38.  
  39.     print("\n")
  40.  
  41. print(winner)
Tags: python ptt
Advertisement
Add Comment
Please, Sign In to add comment