欢迎光临散文网 会员登陆 & 注册

Python超强爬虫8天速成(完整版)爬取各种网站数据实战案例

2023-03-10 20:22 作者:佛系丨少年  | 我要投稿

P57 12306模拟登陆(滑动验证)

"""
@Author:lalala
@File:07.12306模拟登陆.py
@Date:2023/03/10
"""
import time
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver import ChromeOptions
# 配置浏览器的设置
option = ChromeOptions()
# 规避selenium被检测到的风险
option.add_experimental_option('excludeSwitches',['enable-automation'])
option.add_argument('--disable-blink-features=AutomationControlled')
# 定义chrome驱动地址
path = Service('chromedriver.exe')
# 创建浏览器操作对象
browser = webdriver.Chrome(service=path,options=option)
# 设置隐式等待时间,5s内找到了元素就继续执行,找不到报超时(为了防止找元素的时候元素还没加载出来)
browser.implicitly_wait(5)

# 访问网站
browser.get('https://kyfw.12306.cn/otn/resources/login.html')
# 定位输入账号密码,点击登陆
browser.find_element(By.ID, "J-userName").send_keys("xxxx")
browser.find_element(By.ID, "J-password").send_keys("xxxx")
browser.find_element("id", "J-login").click()
# 获取滑块和滑块条
# slider = browser.find_element("class name","nc_iconfont")
slider = browser.find_element(By.XPATH,'//*[@id="nc_1_n1z"]')
slider_length = browser.find_element("class name","nc-lang-cnt")
# print(slider_length.size)
# 创建动作链,长按点击滑块,拖动滑块
action = ActionChains(browser)
action.click_and_hold(slider)
action.move_by_offset(slider_length.size['width'], 0).perform()
action.release()
time.sleep(5)
browser.quit()


Python超强爬虫8天速成(完整版)爬取各种网站数据实战案例的评论 (共 条)

分享到微博请遵守国家法律