Skip to main content
 首页 » 编程设计

Python 脚本之帐户生成器

2024年12月31日11xiaohuochai

我最近才开始学习 python,我尝试为随机商店编写一个帐户生成器,只是出于学习目的,我有几个问题。

这是我的代码:

import requests 
import random 
from random import getrandbits 
import time 
 
session = requests.session() 
 
url = 'https://chmielna20.pl/en/register' 
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36'} 
 
name = 'John' #enter your name 
surname = 'Doe' #enter your surname 
email = 'test+{}@gmail.com'.format(getrandbits(40)) #enter your email, dont     change anything after '+' 
password = 'Testing1234' #enter your password, minimum 8 characters 
 
times = int(input("[" + (time.strftime("%H:%M:%S") + "]" + " - Enter the number of account(s) you would like to create: "))) 
 
text_file = open("chmielna accounts.txt", "w") 
 
 
def create_account(): 
 
print("[" + (time.strftime("%H:%M:%S")) + "]" + " - SUBMITTING INFO.....") 
global session 
global email 
 
    payload = { 
        "Name": name, 
        "surname": surname, 
        "email": email, 
        "password": password, 
        "zapoznalem": "on" 
} 
 
response = session.post(url, data = payload, headers = headers) 
 
def write(): 
text_file.write(email + ":" + password + "\n") 
 
if "Success" in response.text: 
    print("[" + (time.strftime("%H:%M:%S")) + "]" +" - SUCCESSFULLY CREATED     ACCOUNT "+email+":"+password) 
    write() 
else: 
    print("[" + (time.strftime("%H:%M:%S")) + "]" +" - ERROR COULD NOT CREATE "+email+":"+password) 
 
for i in range (times): 
create_account() 

所以第一个问题是为什么我会收到错误“未使用的变量‘payload’”(在 def create_account 中)和“ undefined variable ‘payload’”(作为响应)。我在 def create_account 中定义了 payload。

第二个问题在https://chmielna20.pl/en/register您必须选中“Zapoznałam/em się z Regulaminem sklepu internetowego i akceptuję jego postanowienia”框。 - 我如何在 Python 中做到这一点?

感谢您的帮助!

请您参考如下方法:

问题:函数内部的缩进不正确也不一致。

错误来自 response = session.post(url, data = payload, headers = headers) 行。


使用它并调整您的代码:

import requests 
import random 
from random import getrandbits 
import time 
 
session = requests.session() 
 
url = 'https://chmielna20.pl/en/register' 
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36'} 
 
name = 'John' #enter your name 
surname = 'Doe' #enter your surname 
email = 'test+{}@gmail.com'.format(getrandbits(40)) #enter your email, dont     change anything after '+' 
password = 'Testing1234' #enter your password, minimum 8 characters 
 
times = int(input("[" + (time.strftime("%H:%M:%S") + "]" + " - Enter the number of account(s) you would like to create: "))) 
text_file = open("chmielna accounts.txt", "w") 
 
def create_account(): 
    print("[" + (time.strftime("%H:%M:%S")) + "]" + " - SUBMITTING INFO.....") 
    global session 
    global email 
    payload = { 
        "Name": name, 
        "surname": surname, 
        "email": email, 
        "password": password, 
        "zapoznalem": "on" 
    } 
 
def write(): 
    text_file.write(email + ":" + password + "\n") 
    if "Success" in response.text: 
        print("[" + (time.strftime("%H:%M:%S")) + "]" +" - SUCCESSFULLY CREATED     ACCOUNT "+email+":"+password) 
        write() 
    else: 
        print("[" + (time.strftime("%H:%M:%S")) + "]" +" - ERROR COULD NOT CREATE "+email+":"+password) 
 
for i in range (times): 
    create_account() 

结果:

[00:33:04] - Enter the number of account(s) you would like to create: 5 
[00:33:05] - SUBMITTING INFO..... 
[00:33:05] - SUBMITTING INFO..... 
[00:33:05] - SUBMITTING INFO..... 
[00:33:05] - SUBMITTING INFO..... 
[00:33:05] - SUBMITTING INFO.....