Tạo tài khoản Gmail theo cách thủ công có thể tẻ nhạt, đặc biệt nếu bạn cần nhiều tài khoản để thử nghiệm hoặc cho các mục đích khác. Trong hướng dẫn này, bạn sẽ tìm hiểu cách tự động hóa quy trình đăng ký tài khoản Gmail bằng Python và Selenium. Ngoài ra, chúng tôi sẽ sử dụng faker
thư viện để tạo dữ liệu người dùng ngẫu nhiên, random
để tạo mật khẩu và webdriver-manager
Và PySocks
để xử lý proxy.
Điều kiện tiên quyết
Trước khi chúng tôi bắt đầu, hãy đảm bảo bạn đã cài đặt những thứ sau:
- Python 3.x
- Selen
- Trình điều khiển web Chrome
- kẻ giả mạo
- PyTất
- Trình quản lý WebDriver cho Selenium
Bạn có thể cài đặt Selenium, Faker, PySocks và WebDriver Manager bằng pip:
pip install selenium faker pysocks webdriver-manager
Tải xuống Chrome WebDriver từ đây và đảm bảo rằng nó nằm trong ĐƯỜNG của bạn.
Tổng quan về kịch bản
Kịch bản sẽ:
- Tạo ngẫu nhiên tên, họ, tên người dùng và mật khẩu.
- Mở trang đăng ký Gmail bằng Selenium.
- Điền vào mẫu đăng ký với dữ liệu được tạo.
- Gửi biểu mẫu.
- Sử dụng proxy SOCKS5 cho quá trình đăng ký.
Bước 1: Nhập thư viện và khởi tạo Faker
Đầu tiên, chúng ta sẽ nhập các thư viện cần thiết và khởi tạo Faker
:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from faker import Faker
import random
import string
import time
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
# Initialize Faker
fake = Faker()
Bước 2: Tạo dữ liệu ngẫu nhiên
Tiếp theo, chúng ta sẽ tạo một hàm để tạo mật khẩu ngẫu nhiên và tạo dữ liệu người dùng ngẫu nhiên:
# Function to generate a random password
def generate_password(length=12):
characters = string.ascii_letters + string.digits + string.punctuation
password = ''.join(random.choice(characters) for i in range(length))
return password
# Generate random user data
first_name = fake.first_name()
last_name = fake.last_name()
username = first_name.lower() + last_name.lower() + str(random.randint(1000, 9999))
password = generate_password()
Bước 3: Khởi tạo Selenium WebDriver bằng Proxy
Khởi tạo Chrome WebDriver và điều hướng đến trang đăng ký Gmail, sử dụng proxy SOCKS5:
# Proxy settings (replace with your proxy details)
proxy = "your_proxy_address:your_proxy_port"
# Initialize Chrome WebDriver with proxy settings
chrome_options = Options()
chrome_options.add_argument("--proxy-server=socks5://" + proxy)
# Initialize the WebDriver
service = Service(ChromeDriverManager().install())
driver = webdriver.Chrome(service=service, options=chrome_options)
# Open Gmail signup page
driver.get("https://accounts.google.com/signup")
Bước 4: Điền vào mẫu đăng ký
Đợi trang tải, xác định vị trí các trường biểu mẫu và điền dữ liệu được tạo vào chúng:
# Wait for the page to load and locate the form fields
wait = WebDriverWait(driver, 10)
first_name_field = wait.until(EC.presence_of_element_located((By.ID, "firstName")))
last_name_field = driver.find_element(By.ID, "lastName")
username_field = driver.find_element(By.ID, "username")
password_field = driver.find_element(By.NAME, "Passwd")
confirm_password_field = driver.find_element(By.NAME, "ConfirmPasswd")
# Fill out the form fields
first_name_field.send_keys(first_name)
last_name_field.send_keys(last_name)
username_field.send_keys(username)
password_field.send_keys(password)
confirm_password_field.send_keys(password)
Bước 5: Gửi biểu mẫu
Gửi biểu mẫu và thêm độ trễ để cho phép tải trang tiếp theo:
# Submit the form
next_button = driver.find_element(By.XPATH, '//*[@id="accountDetailsNext"]/div/button')
next_button.click()
# Add a delay to allow the next page to load (you may need to adjust the sleep time)
time.sleep(5)
Bước 6: Xử lý các bước bổ sung
Tùy thuộc vào quy trình đăng ký của Google, bạn có thể cần xử lý các bước bổ sung như xác minh số điện thoại, email khôi phục hoặc CAPTCHA. Phần này sẽ khác nhau và có thể yêu cầu xử lý nâng cao hơn.
Bước 7: Đóng trình duyệt
Đóng trình duyệt sau khi quá trình hoàn tất:
# Close the browser after the process is complete
driver.quit()
# Output the generated data
print(f"First Name: {first_name}")
print(f"Last Name: {last_name}")
print(f"Username: {username}")
print(f"Password: {password}")
Kịch bản đầy đủ
Đây là tập lệnh đầy đủ kết hợp tất cả các bước:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from faker import Faker
import random
import string
import time
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
# Initialize Faker
fake = Faker()
# Function to generate a random password
def generate_password(length=12):
characters = string.ascii_letters + string.digits + string.punctuation
password = ''.join(random.choice(characters) for i in range(length))
return password
# Generate random user data
first_name = fake.first_name()
last_name = fake.last_name()
username = first_name.lower() + last_name.lower() + str(random.randint(1000, 9999))
password = generate_password()
# Proxy settings (replace with your proxy details)
proxy = "your_proxy_address:your_proxy_port"
# Initialize Chrome WebDriver with proxy settings
chrome_options = Options()
chrome_options.add_argument("--proxy-server=socks5://" + proxy)
# Initialize the WebDriver
service = Service(ChromeDriverManager().install())
driver = webdriver.Chrome(service=service, options=chrome_options)
# Open Gmail signup page
driver.get("https://accounts.google.com/signup")
# Wait for the page to load and locate the form fields
wait = WebDriverWait(driver, 10)
first_name_field = wait.until(EC.presence_of_element_located((By.ID, "firstName")))
last_name_field = driver.find_element(By.ID, "lastName")
username_field = driver.find_element(By.ID, "username")
password_field = driver.find_element(By.NAME, "Passwd")
confirm_password_field = driver.find_element(By.NAME, "ConfirmPasswd")
# Fill out the form fields
first_name_field.send_keys(first_name)
last_name_field.send_keys(last_name)
username_field.send_keys(username)
password_field.send_keys(password)
confirm_password_field.send_keys(password)
# Submit the form
next_button = driver.find_element(By.XPATH, '//*[@id="accountDetailsNext"]/div/button')
next_button.click()
# Add a delay to allow the next page to load (you may need to adjust the sleep time)
time.sleep(5)
# Close the browser after the process is complete
driver.quit()
# Output the generated data
print(f"First Name: {first_name}")
print(f"Last Name: {last_name}")
print(f"Username: {username}")
print(f"Password: {password}")
Phần kết luận
Bạn đã tự động hóa thành công quy trình đăng ký tài khoản Gmail bằng Python và Selenium, cùng với việc bổ sung hỗ trợ proxy SOCKS5. Tập lệnh này tạo dữ liệu người dùng ngẫu nhiên, điền vào biểu mẫu đăng ký và định tuyến lưu lượng truy cập thông qua một proxy được chỉ định.
Nhớ thay thế your_proxy_address:your_proxy_port
với các chi tiết proxy thực tế của bạn và sử dụng tập lệnh này một cách có trách nhiệm, hãy ghi nhớ các ý nghĩa pháp lý và đạo đức của việc tự động tạo tài khoản.