Python Selenium 自动登入1688

Python Selenium是一个用于自动化Web浏览器操作的库。它提供了一组功能强大的工具和API,可以模拟用户在浏览器中的行为,并执行各种任务,如点击、输入文本、提交表单等。

要使用Python Selenium登录1688网站,需要进行以下步骤:

  1. 安装Selenium库:在Python环境中,可以使用pip命令安装Selenium库。

    python 复制代码
    pip install selenium
  2. 下载并配置浏览器驱动程序:Selenium需要一个特定的浏览器驱动程序来与浏览器交互。对于Chrome浏览器,可以下载ChromeDriver;对于Firefox浏览器,可以下载geckodriver。将驱动程序放在系统路径中,或者指定其路径。

  3. 导入必要的模块:在Python脚本中,首先要导入selenium模块,以及WebDriver对象(用于控制浏览器)和Keys对象(用于模拟键盘输入)。

    python 复制代码
    from selenium import webdriver
    from selenium.webdriver.common.action_chains import ActionChains
    from selenium.webdriver.common.by import By
  4. 创建WebDriver实例:根据使用的浏览器类型,创建相应的WebDriver实例。例如,对于Chrome浏览器:

    python 复制代码
    driver = webdriver.Chrome()
  5. 打开1688网站:使用WebDriver打开1688网站的登录页面。

    python 复制代码
    driver.get("https://www.1688.com/")
  6. 定位并输入登录凭据:使用WebDriver的find_element方法找到用户名和密码的输入框,并使用send_keys方法输入相应的凭据。

  7. 提交登录表单:使用WebDriver的submit方法提交登录表单。

以下是完整代码:

python 复制代码
import time
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By

print('使用selenium模拟登陆')
# 使用selenium模拟登陆,获取并返回cookie
username = ''
password = ''
options = webdriver.ChromeOptions()  # 声明浏览器的对象
options.add_experimental_option('excludeSwitches', ['enable-automation'])
options.add_argument('--disable-blink-features')
options.add_argument('--disable-blink-features=AutomationControlled')  # 去除浏览器selenium监控
#options.add_argument('--headless')  # 浏览器不提供可视化页面
options.add_argument('--disable-gpu')  # 禁用GPU加速
browser = webdriver.Chrome(options=options)
browser.get('https://login.1688.com/')
time.sleep(1)
browser.find_element(By.XPATH, '//*[@id="fm-login-id"]').send_keys(username)
time.sleep(1)
browser.find_element(By.XPATH, '//*[@id="fm-login-password"]').send_keys(password)
time.sleep(1)
browser.find_element(By.XPATH, '//*[@type="submit"]').click()
time.sleep(4)
# 检查是否出现了滑动验证码
try:
    slider = browser.find_element(By.XPATH,  '//*[@id="nc_1_n1z"]')
    ActionChains(browser).click_and_hold(slider).perform()  # 按住滑块
    ActionChains(browser).move_by_offset(300, 0).perform()  # 移动到最右边
    ActionChains(browser).pause(0.5).release().perform()  # 松开鼠标
except:
    pass
# cookies_dict = {cookie['name']: cookie['value'] for cookie in driver.get_cookies()}
# driver.quit()
# print(cookies_dict)
相关推荐
leisoo80976 小时前
100GBA股股票数据怎么存ClickHouseRedisMySQLJSON完整对比
大数据·linux·服务器·开发语言·python
WangYan20226 小时前
基于XGBoost与AI的生态—地学多源数据建模:植被与土地利用识别、土壤碳氮空间预测、生物多样性驱动机制、土壤微生物功能预测、生态退化与风险识别
python·机器学习·xgboost
金銀銅鐵8 小时前
[Python] 借助 turtle 逐字展示唐诗《金缕衣》
python
Python大数据分析@10 小时前
使用大模型MCP采集数据,爬虫已经无门槛
python·网络爬虫
quantdash_cc10 小时前
告别自建 Requests/BS4 网页爬虫:基于 QuantDash 搭建零维保的高性能量化行情流水线
开发语言·爬虫·python·pandas·量化·quantdash
65岁退休Coder11 小时前
LangChain v1.3.4 笔记 - 07 补充:链式调用 LCEL
后端·python·langchain
卷无止境11 小时前
FastAPI 部署在 Nginx 后面到底该怎么配
后端·python
半亩码田11 小时前
C#转Python第3.1篇:Python 的 class 没有访问修饰符?面向对象的另一条路
开发语言·python·c#
Wzx19801212 小时前
python沙箱和docker沙箱你选对了吗?
开发语言·python·docker
Python私教12 小时前
签名 URL 刷新导致审批失效?别急着删掉所有 query
python·安全