如何遍历邮箱列表批量执行表单自动填充提交脚本?
Absolutely! You can easily tweak your script to loop through a list of emails and submit the form automatically for each address. Here's a straightforward solution tailored to your existing code:
Step 1: Set Up Your Email List
First, gather all the emails you want to use—you can either hardcode them directly in the script or load them from a file (great for large lists).
Step 2: Modify the Script for Looped Submissions
We'll use a reusable base payload (so you don't repeat redundant code) and update only the $email field for each iteration. We'll also add basic error handling to keep the loop running even if one submission hits a snag.
import requests # Define your list of target emails email_list = [ "sampleemail1@gmail.com", "sampleemail2@yahoo.com", "sampleemail3@outlook.com", # Add as many emails as you need here ] # Base payload (we'll reuse this and update only the email each time) base_payload = { 'g': 'UhQRUE', '$fields':'$source,$email,$consent_method,$consent_form_id,$consent_form_version,services', '$list_fields': '', '$timezone_offset':'-5', '$source':'Gift Card Giveaway Form', '$consent_method': "Klaviyo Form", '$consent_form_id': 'WbvfNT', '$consent_form_version': '1842792', 'services': '{"shopify":{"source":"form"}}', } # Loop through each email and submit the form for email in email_list: # Create a copy of the base payload to avoid overwriting it payload = base_payload.copy() payload['$email'] = email try: # Send the POST request response = requests.post("https://a.klaviyo.com/ajax/subscriptions/subscribe", data=payload) # Check if the submission was successful if response.ok: print(f✅ Successfully submitted form for {email}") else: print(f❌ Failed to submit for {email}. Status code: {response.status_code}") except Exception as e: print(f⚠️ Error processing {email}: {str(e)}")
Bonus: Load Emails from a Text File
If you have a large number of emails stored in a text file (one email per line), you can load them dynamically instead of hardcoding:
# Load emails from a text file (one email per line) with open('emails.txt', 'r') as file: email_list = [line.strip() for line in file if line.strip()]
Quick Tips:
- Using
base_payload.copy()is crucial because dictionaries are mutable—this ensures we don't accidentally modify the original base payload for subsequent iterations. - The
try/exceptblock prevents a single failure (like a network glitch) from stopping the entire loop. response.okis a simple way to check if the request succeeded (it verifies status codes between 200-299).
内容的提问来源于stack exchange,提问作者user14657232

