我最近才开始学习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()
因此,第一个问题是为什么我会得到错误“未使用的变量”有效载荷”(在def create_account中)和“未定义的变量”有效载荷”(作为响应)。我在def create_account中定义了有效负载。
第二个问题是https://chmielna20.pl/en/register,您必须选中“Zapoznałam/ emsięz Regulaminem sklepu internetowego”。 -如何在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.....