下载地址:https://www.pan38.com/dow/share.php?code=JCnzE 提取密码:1133
这段代码实现了拼多多的自动化操作流程,包含登录、搜索商品、选择商品和下单功能。代码使用了Selenium进行网页自动化操作,并添加了随机延迟模拟人类操作。请注意这仅用于技术学习。
import time
import random
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException, NoSuchElementException
class PDDOrderAutomation:
def __init__(self):
self.chrome_options = Options()
self.chrome_options.add_argument("--disable-notifications")
self.chrome_options.add_argument("--disable-infobars")
self.chrome_options.add_argument("--disable-extensions")
self.chrome_options.add_argument("--disable-gpu")
self.chrome_options.add_argument("--no-sandbox")
self.driver = webdriver.Chrome(options=self.chrome_options)
self.wait = WebDriverWait(self.driver, 20)
def login(self, username, password):
try:
self.driver.get("https://mobile.yangkeduo.com/login.html")
time.sleep(random.uniform(2, 4))
# 切换账号密码登录
self.wait.until(EC.element_to_be_clickable(
(By.XPATH, "//div[contains(text(),'账号密码登录')]")
)).click()
# 输入用户名
username_field = self.wait.until(EC.presence_of_element_located(
(By.XPATH, "//input[@placeholder='请输入手机号']")
))
self.slow_type(username_field, username)
# 输入密码
password_field = self.driver.find_element(
By.XPATH, "//input[@placeholder='请输入密码']"
)
self.slow_type(password_field, password)
# 点击登录
login_btn = self.driver.find_element(
By.XPATH, "//button[contains(text(),'登录')]"
)
login_btn.click()
# 等待登录成功
time.sleep(random.uniform(5, 8))
return True
except Exception as e:
print(f"登录失败: {str(e)}")
return False
def search_product(self, keyword):
try:
search_box = self.wait.until(EC.presence_of_element_located(
(By.XPATH, "//input[@placeholder='搜索商品']")
))
self.slow_type(search_box, keyword)
search_box.send_keys(Keys.RETURN)
time.sleep(random.uniform(3, 5))
return True
except Exception as e:
print(f"搜索商品失败: {str(e)}")
return False
def select_product(self, index=0):
try:
products = self.wait.until(EC.presence_of_all_elements_located(
(By.XPATH, "//div[contains(@class,'goods-item')]")
))
if len(products) > index:
products[index].click()
time.sleep(random.uniform(3, 5))
return True
return False
except Exception as e:
print(f"选择商品失败: {str(e)}")
return False
def place_order(self):
try:
# 点击立即购买
buy_btn = self.wait.until(EC.element_to_be_clickable(
(By.XPATH, "//button[contains(text(),'立即购买')]")
))
buy_btn.click()
time.sleep(random.uniform(2, 4))
# 确认订单
confirm_btn = self.wait.until(EC.element_to_be_clickable(
(By.XPATH, "//button[contains(text(),'提交订单')]")
))
confirm_btn.click()
time.sleep(random.uniform(3, 5))
return True
except Exception as e:
print(f"下单失败: {str(e)}")
return False
def slow_type(self, element, text):
for char in text:
element.send_keys(char)
time.sleep(random.uniform(0.1, 0.3))
def run(self, username, password, keyword):
try:
if not self.login(username, password):
return False
if not self.search_product(keyword):
return False
if not self.select_product():
return False
if not self.place_order():
return False
return True
except Exception as e:
print(f"自动化流程出错: {str(e)}")
return False
finally:
self.driver.quit()
if __name__ == "__main__":
automation = PDDOrderAutomation()
automation.run(
username="your_username",
password="your_password",
keyword="手机"
)
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。