随手写了一个python脚本,为了检测搬瓦工邀请码的有效性。找一个闲置服务器,直接在Linux上运行。
Python环境安装
apt update
apt install python3 python3-pip -y
安装 requests 库
apt install python3-requests
功能实现:
- 检测邀请码有效性
- 将有效和无效的邀请码分别输出到不同文件
- 从原始列表中自动删除无效的邀请码(写入新的“有效邀请码列表”)
- 避免重复检测(去重)
Python 脚本,随便命名 bwhcodes.py
import time
import requests
class InviteCodeChecker:
def __init__(self):
self.session = requests.Session()
self.headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
'Content-Type': 'application/x-www-form-urlencoded'
}
def check_code(self, invite_code, retries=3):
for attempt in range(retries):
try:
self.session.get('https://bwh81.net/aff.php?aff=1&pid=158', headers=self.headers)
self.session.get('https://bwh81.net/cart.php?a=add&pid=158', headers=self.headers)
cart_url = 'https://bwh81.net/cart.php?a=view'
data = {'invite_code': invite_code}
response = self.session.post(cart_url, headers=self.headers, data=data)
return 'errorbox' not in response.text
except Exception as e:
print(f"[{attempt+1}/{retries}] 请求出错: {str(e)}")
time.sleep(2)
return False
def main():
# 从文件读取邀请码(每行一个)
with open("invite_codes.txt", "r") as f:
codes = [line.strip() for line in f if line.strip()]
codes = list(set(codes)) # 去重
checker = InviteCodeChecker()
valid_codes = []
invalid_codes = []
for code in codes:
print(f"检测中: {code}")
is_valid = checker.check_code(code)
if is_valid:
print(f"✅ 有效: {code}")
valid_codes.append(code)
else:
print(f"❌ 无效: {code}")
invalid_codes.append(code)
# 等待 5 秒防止请求太频繁
time.sleep(5)
# 写入有效邀请码
with open("valid_invite_codes.txt", "w") as f:
for code in valid_codes:
f.write(code + "\n")
# 写入无效邀请码(可选)
with open("invalid_invite_codes.txt", "w") as f:
for code in invalid_codes:
f.write(code + "\n")
# 更新原始邀请码文件,仅保留有效的
with open("invite_codes.txt", "w") as f:
for code in valid_codes:
f.write(code + "\n")
print(f"\n🎉 检测完成:有效 {len(valid_codes)} 个,无效 {len(invalid_codes)} 个")
print("✅ 已更新 invite_codes.txt,仅保留有效邀请码")
if __name__ == "__main__":
main()
说明:
示例用法:
你只需要同路径下创建一个 invite_codes.txt
文件,比如:
bwh_ABC123_aff62581
bwh_DEF456_aff62581
bwh_GHI789_aff62581
然后运行脚本即可。
python3 bwhcodes.py