- 前言
- 一、代码公示
- 二、程序模板
- 1.引入库
- 2.大致思路:
- 3.完整代码
- 总结
前言
本篇文章记录了本人用于LOFTER粉丝抽奖的一段小程序,抽奖代码公示于此供大家监督。
文末附有相应模板,有同样需求的小伙伴可以自行取用。
一、代码公示
语言环境:python 3.9
编译平台:PyCharm Community Edition 2020.3.3
此代码是本人用于LOFTER抽奖的一个小程序,公示于此供大家监督,大家可自行运行验证。
代码如下:
import requests
from bs4 import BeautifulSoup
import random
import time
import sys
import os
def aligns(string, length=20):
difference = length - len(string) # 计算限定长度为20时需要补齐多少个空格
if difference == 0: # 若差值为0则不需要补
return string
elif difference < 0:
print('错误:限定的对齐长度小于字符串长度!')
return None
new_string = ''
space = ' '
for m in string:
codes = ord(m) # 将字符转为ASCII或UNICODE编码
if codes <= 126: # 若是半角字符
new_string = new_string + chr(codes + 65248) # 则转为全角
else:
new_string = new_string + m # 若是全角,则不转换
return new_string + space * difference # 返回补齐空格后的字符串
req = requests.get(url="https://dooooooooooog.lofter.com/post/1d40af8b_2b533dba8")
req.encoding = "utf-8"
soup = BeautifulSoup(req.text, features="html.parser")
company_items = soup.find_all("li", class_="note share")
company_items.reverse()
i = int(1)
aa = {}
TXT = open('LOFTER抽奖[300粉]成功参与人员及中奖人员名单.txt', mode='w', encoding='utf-8')
print("截至2021年4月25日24时整,LOFTER抽奖[300粉]成功参与人员如下:n", file=TXT)
TXT.close()
print("nnt截至2021年4月25日24时整,LOFTER抽奖[300粉]成功参与人员如下:n")
for company_item in company_items:
dd = company_item.text.strip()
dd = dd.replace(' 推荐了此图片', '')
aa[i] = dd
if i < 10:
print("t" + '0', end='')
else:
print("t", end='')
print(str(i) + " " + "@" + aligns(dd, 10), end='')
if i % 3 == 0:
print()
TXT = open('LOFTER抽奖[300粉]成功参与人员及中奖人员名单.txt', mode='a', encoding='utf-8')
if i < 10:
print("t" + '0', end='', file=TXT)
else:
print("t", end='', file=TXT)
print(str(i) + " " + '@' + aligns(dd, 10), end='', file=TXT)
if i % 3 == 0:
print('', file=TXT)
TXT.close()
i = i + 1
input("nnt请戳回车开始抽奖")
os.system('cls')
for t in [5, 4, 3, 2, 1]:
sys.stdout.write("nnnntt")
sys.stdout.flush()
sys.stdout.write('%6d' % t)
sys.stdout.flush()
time.sleep(1)
os.system('cls')
L1 = random.sample(range(1, i), 5)
print("nnt获得实体章的两位小可爱分别是:")
time.sleep(1)
a = L1[0]
if a < 10:
print("ntt" + '0', end='')
else:
print("ntt", end='')
print(str(a) + " " + "@" + aa[a])
time.sleep(1)
b = L1[1]
if b < 10:
print("tt" + '0', end='')
else:
print("tt", end='')
print(str(b) + " " + "@" + aa[b])
time.sleep(1)
print("nt获得电子章的三位小可爱分别是:")
time.sleep(1)
c = L1[2]
if c < 10:
print("ntt" + '0', end='')
else:
print("ntt", end='')
print(str(c) + " " + "@" + aa[c])
time.sleep(1)
d = L1[3]
if d < 10:
print("tt" + '0', end='')
else:
print("tt", end='')
print(str(d) + " " + "@" + aa[d])
time.sleep(1)
e = L1[4]
if e < 10:
print("tt" + '0', end='')
else:
print("tt", end='')
print(str(e) + " " + "@" + aa[e])
TXT = open('LOFTER抽奖[300粉]成功参与人员及中奖人员名单.txt', mode='a', encoding='utf-8')
print("", file=TXT)
print("n获得实体章的两位小可爱分别是:", file=TXT)
if a < 10:
print("t" + '0', end='', file=TXT)
else:
print("t", end='', file=TXT)
print(str(a) + " " + "@" + aa[a], file=TXT)
if b < 10:
print("t" + '0', end='', file=TXT)
else:
print("t", end='', file=TXT)
print(str(b) + " " + "@" + aa[b], file=TXT)
print("", file=TXT)
print("获得电子章的三位小可爱分别是:", file=TXT)
if c < 10:
print("t" + '0', end='', file=TXT)
else:
print("t", end='', file=TXT)
print(str(c) + " " + "@" + aa[c], file=TXT)
if d < 10:
print("t" + '0', end='', file=TXT)
else:
print("t", end='', file=TXT)
print(str(d) + " " + "@" + aa[d], file=TXT)
if e < 10:
print("t" + '0', end='', file=TXT)
else:
print("t", end='', file=TXT)
print(str(e) + " " + "@" + aa[e], file=TXT)
TXT.close()
time.sleep(3)
os.system('cls')
print("nnnt抽奖结束啦,恭喜中奖的各位!!!撒花!*★,°*:.☆\( ̄▽ ̄)/$:*.°★*")
time.sleep(5)
运行截图:
如下是需要用到的几个库,如有缺少请自行下载。
import requests from bs4 import BeautifulSoup import random import time import sys import os2.大致思路:
- 从相应网站爬取成功参与人员名单并打印于控制台,同时将其写入文档;
- 生成随机数,与之相对应的即为中奖人员;
- 将中奖人员打印于控制台,同时写入文档。
import requests
from bs4 import BeautifulSoup
import random
import time
import sys
import os
url0 = str('**********************') # 此处需输入相应的网站
file_name = str('123456.txt') # 可自定义保存抽奖信息的文档名称
D = str('*****在此输入需删除的内容****') # 从网页中提取的内容如有不需要的字符可在此删除
num = int(5) # 此处定义中奖人数
def aligns(string, length=20): # length即为输出时每个人员所占用的字符长度,可自行调整,*注意length不可小于字符串长度
difference = length - len(string)
if difference == 0:
return string
elif difference < 0:
print('错误:限定的对齐长度小于字符串长度!')
return None
new_string = ''
space = ' '
for m in string:
codes = ord(m)
if codes <= 126:
new_string = new_string + chr(codes + 65248)
else:
new_string = new_string + m
return new_string + space * difference
req = requests.get(url=url0)
req.encoding = "utf-8"
soup = BeautifulSoup(req.text, features="html.parser")
company_items = soup.find_all("***", class_="********") # 此处需输入相应的标记名与属性,详情可参考CSDN博主liver100day的文章:
# 文章链接:https://blog.csdn.net/liver100day/article/details/121258749
company_items.reverse() # 此函数可将读取到的人员名单逆序排列,根据个人需要选择是否使用即可
i = int(1)
aa = {}
TXT = open(file_name, mode='w', encoding='utf-8')
print("截至****年**月**日**时整,*******抽奖成功参与人员如下:n", file=TXT)
TXT.close()
print("nnt截至****年**月**日**时整,*******抽奖成功参与人员如下:n")
for company_item in company_items:
dd = company_item.text.strip()
dd = dd.replace(D, '')
aa[i] = dd
if i < 10:
print("t" + '0', end='')
else:
print("t", end='')
print(str(i) + " " + "@" + aligns(dd), end='')
if i % 3 == 0:
print()
TXT = open(file_name, mode='a', encoding='utf-8')
if i < 10:
print("t" + '0', end='', file=TXT)
else:
print("t", end='', file=TXT)
print(str(i) + " " + '@' + aligns(dd), end='', file=TXT)
if i % 3 == 0:
print('', file=TXT)
TXT.close()
i = i + 1
input("nnt请戳回车开始抽奖")
os.system('cls')
for t in [5, 4, 3, 2, 1]: # 倒数五个数,可自行修改
sys.stdout.write("nnnntt")
sys.stdout.flush()
sys.stdout.write('%6d' % t)
sys.stdout.flush()
time.sleep(1)
os.system('cls')
L1 = random.sample(range(1, i), num)
print("nnt中奖的%d位小可爱分别是:" % num)
time.sleep(1)
TXT = open(file_name, mode='a', encoding='utf-8')
print("", file=TXT)
print("n获得实体章的两位小可爱分别是:", file=TXT)
for a in L1:
if a < 10:
print("ntt" + '0', end='')
else:
print("ntt", end='')
print(str(a) + " " + "@" + aa[a])
if a < 10:
print("t" + '0', end='', file=TXT)
else:
print("t", end='', file=TXT)
print(str(a) + " " + "@" + aa[a], file=TXT)
time.sleep(1)
time.sleep(3)
os.system('cls')
print("nnnt抽奖结束啦,恭喜中奖的各位!!!撒花!*★,°*:.☆\( ̄▽ ̄)/$:*.°★*")
time.sleep(5)
总结
本人非计算机专业,程序如有不规范之处还请见谅。
程序中获取网页内容部分参考博主liver100day的文章,原文为:如何用Python获取网页指定内容
其中所用aligns函数为LingMessy博主编写,原文为:python - 实现中文字符串对齐的方法
LOFTER账号同名
ID:dooooooooooog
是一个佛系刻戳子的渣渣,欢迎来戳:https://dooooooooooog.lofter.com/。



