一. 首先安装python和pycharm

1.在官网进行下载python
windows下载链接: https://www.python.org/downloads/windows/.
点击查看详细安装教程

2.官网下载并且安装pycharm https://www.jetbrains.com/pycharm/.点击查看详细安装教程

二. 安装谷歌浏览器和相应版本的驱动

  1. 查看谷歌浏览器的版本
    ![在这里插入图片描述](https://img-blog.csdnimg.cn/20210523190904883.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2h6bmJfMzY5,size_16,color_FFFFFF,t_70
  2. 下载响应的谷歌驱动链接: https://npm.taobao.org/mirrors/chromedriver.
  3. 安装完成之后找到chromedriver.exe文件复制到项目中Alt

三. 超级鹰的使用

  1. 打开链接注册: https://www.chaojiying.com/.
  2. 打开个人中心,点击 软件ID 在这里插入图片描述
  3. 点击生成一个软件ID
  4. 点击查看详细的使用方法.

四. 关于12306的使用

  1. 首先进行12306的账户注册
  2. 登录界面如下在这里插入图片描述
  3. 本案例使用 ——账号密码登录
  4. 登录链接: https://kyfw.12306.cn/otn/resources/login.html.

五. 代码实现

  1. 注册
  2. 导入相应的模块
// 导入相应的python库
from selenium.webdriver import Chrome
from chaojiying import Chaojiying_Client
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.chrome.options import Options
import time
  1. 初始化超级鹰
#初始化超级鹰
chaojiying = Chaojiying_Client('账号', '密码', '软件ID')

# 2.chrome的版本大于等于88
option = Options()
# option.add_experimental_option('excludeSwitches', ['enable-automation'])
option.add_argument('--disable-blink-features=AutomationControlled')
  1. 访问12306 点击账号密码登录
web = Chrome(options=option)
web.get("https://kyfw.12306.cn/otn/resources/login.html")
time.sleep(2)
web.find_element_by_xpath('/html/body/div[2]/div[2]/ul/li[2]/a').click()
time.sleep(3)
  1. 处理验证码
# 先处理验证码
verify_img_element = web.find_element_by_xpath('//*[@id="J-loginImg"]')
# 用超级鹰去识别验证码
dic = chaojiying.PostPic(verify_img_element.screenshot_as_png, 9004)
result = dic['pic_str']  # x1,y1|x2,y2|x3,y3
rs_list = result.split("|")
for rs in rs_list:  # x1,y1
    p_temp = rs.split(",")
    x = int(p_temp[0])
    y = int(p_temp[1])    ActionChains(web).move_to_element_with_offset(verify_img_element, x, y).click().perform()
time.sleep(2)
  1. 输入账号和密码
#输入账号和密码
web.find_element_by_xpath('//*[@id="J-userName"]').send_keys('账号')
web.find_element_by_xpath('//*[@id="J-password"]').send_keys('密码')
time.sleep(2)
  1. 点击登录
# 点击登录
web.find_element_by_xpath('//*[@id="J-login"]').click()
time.sleep(5)
  1. 拖拽滑块
# 拖拽
btn = web.find_element_by_xpath('//*[@id="nc_1_n1z"]')
ActionChains(web).drag_and_drop_by_offset(btn, 300, 0).perform()

六. 完整代码如下

from selenium.webdriver import Chrome
from chaojiying import Chaojiying_Client
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.chrome.options import Options
import time

#初始化超级鹰
chaojiying = Chaojiying_Client('账号', '密码', '软件ID')

# 2.chrome的版本大于等于88
option = Options()
# option.add_experimental_option('excludeSwitches', ['enable-automation'])
option.add_argument('--disable-blink-features=AutomationControlled')

web = Chrome(options=option)
web.get("https://kyfw.12306.cn/otn/resources/login.html")
time.sleep(2)
web.find_element_by_xpath('/html/body/div[2]/div[2]/ul/li[2]/a').click()
time.sleep(3)
# 先处理验证码
verify_img_element = web.find_element_by_xpath('//*[@id="J-loginImg"]')
# 用超级鹰去识别验证码
dic = chaojiying.PostPic(verify_img_element.screenshot_as_png, 9004)
result = dic['pic_str']  # x1,y1|x2,y2|x3,y3
rs_list = result.split("|")
for rs in rs_list:  # x1,y1
    p_temp = rs.split(",")
    x = int(p_temp[0])
    y = int(p_temp[1])    ActionChains(web).move_to_element_with_offset(verify_img_element, x, y).click().perform()
time.sleep(2)
#输入账号和密码
web.find_element_by_xpath('//*[@id="J-userName"]').send_keys('账号')
web.find_element_by_xpath('//*[@id="J-password"]').send_keys('密码')
time.sleep(2)
# 点击登录
web.find_element_by_xpath('//*[@id="J-login"]').click()
time.sleep(5)
# 拖拽
btn = web.find_element_by_xpath('//*[@id="nc_1_n1z"]')
ActionChains(web).drag_and_drop_by_offset(btn, 300, 0).perform()
Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐