爬虫_07_12306模拟登录

news/2024/7/19 11:19:55 标签: python, 爬虫

07_12306模拟登录

网站地址:https://kyfw.12306.cn/otn/resources/login.html/init

  1. 使用selenium打开登录页面
  2. 对当前selenium打开的这张页面进行截图
  3. 对当前图片局部区域(验证码图片)进行裁剪
    • 好处:将验证码图片和模拟登录进行一一对应
  4. 使用超级鹰识别验证码图片(坐标)
python">#...
#上述代码为超级鹰提供的示例代码

#使用selenium打开登录页面
from selenium import webdriver
import time

bro = webdriver.Chrome(executable_path='./chromedriver')
bro.get('https://kyfw.12306.cn/otn/login/init')
time.sleep(1)

#save_screenshot就是将当前页面进行截图且保存
bro.save_screenshot('aa.png')

#确定验证码图片对应的左上角和右下角的坐标(裁剪的区域就确定)
code_img_ele = bro.find_element_by_xpath('//*[@id="loginForm"]/div/ul[2]/li[4]/div/div/div[3]/img')
location = code_img_ele.location # 验证码图片左上角的坐标x,y
size = code_img_ele.size # 验证码标签对应的长和宽
#左上角和右下角坐标
rangle = (
    int(location['x']),int(location['y']),int(location['x'] + size['width']),int(location['y'] + size['height'])
)
#至此验证码图片区域就确定下来了

from PIL import Image
#
i = Image.open('aa.png')
code_img_name = './code.png'
#crop根据指定区域进行图片裁剪
frame = i.crop(rangle)
frame.save(code_img_name)

#将验证码图片提交给超级鹰进行识别
chaojiying = Chaojiying_Client('账号','密码','secret')
im = open('code.jpg','rb').read()
result = chaojiying.PostPic(im,9004)['pic_str']
print(result)
#锣 253,83|253,153

from selenium.webdriver import ActionChains
all_list = [] #要存储即将被点击的点的坐标 [[x1,y1],[x2,y2]]
if '|' in result:
    list_1 = result.split('|')
    count_1 = len(list_1)
    for i in range(count_1):
        xy_list = []
        x = int(list_1[i].split(',')[0])
        y = int(list_1[i].split(',')[1])
        xy_list.append(x)
        xy_list.append(y)
        all_list.append(xy_list)
else:
    x = int(result.split(',')[0])
    y = int(result.split(',')[1])
    xy_list = []
    xy_list.append(x)
    xy_list.append(y)
    all_list.append(xy_list)
print(all_list)
#遍历列表,使用动作链对每一个列表元素对应的x,y指定的位置进行点击操作
for l in all_list:
    x = l[0]
    y = l[1]
    #相对于参照物(验证码栏)进行点击
    ActionChains(bro).move_to_element_with_offset(code_img_ele,x,y).click().perform()
    time.sleep(0.5)

#录入用户名和密码
bro.find_element_by_id('username').send_keys('xxxxxxx')
time.sleep(2)
bro.find_element_by_id('password').send_keys('xxxxxxx')
time.sleep(2)
bro.find_element_by_id('loginSub').click()
time.sleep(10)
bro.quit()

http://www.niftyadmin.cn/n/1363924.html

相关文章

OOzie 入门 【转】

http://blackproof.iteye.com/blog/1928122 oozie概述: oozie是基于hadoop的调度器,以xml的形式写调度流程,可以调度mr,pig,hive,shell,jar等等。 主要的功能有 Workflow: 顺序执行流…

爬虫_06_余票检测js解加密

06_余票检测&js解加密&12306模拟登录 余票检测1️⃣ JS解密混淆破解 博客地址:https://www.cnblogs.com/bobo-zhang/p/11243138.html爬取的网站:https://www.aqistudy.cn/html/city_detail.html 分析 修改查询条件(城市的名称时间…

asp.net 中使用JQuery Ajax 上传文件

首先创建一个网页&#xff0c;网页中添加如下代码。 <h3>Upload File using Jquery AJAX in Asp.net</h3> <table> <tr> <td>File:</td> <td> <asp:FileUpload ID"fupload" runat"server" οnchangepr…

爬虫_08_scrapy持久化存储管道操作手动请求发送

08_scrapy&持久化存储&管道操作&手动请求发送 scrapy框架 简介 简介&#xff1a;所谓的框架其实就是一个被集成了很多功能且具有很强通用性的一个项目模板。 学习&#xff1a;学习是学好框架中集成好的各种功能、特性 进阶&#xff1a;逐步的探索框架的底层 …

[python]python学习笔记(四)

14&#xff0c;python如何创建进程并在父子进程通信示例代码如下&#xff1a; [cpp] view plaincopyimport os, sys print "Im going to fork now - the child will write something to a pipe, and the parent will read it back" r, w os.pipe() # r,…

爬虫_09_请求传参中间件大文件下载CrawlSpider

09_请求传参&中间件&大文件下载&CrawlSpider 五大核心组件 目的 大概了解scrapy的运行机制为分布式铺垫 请求传参实现的深度爬取 深度爬取&#xff1a;爬取的数据没有在同一张页面中&#xff08;例如首页数据详情页数据&#xff09; 在scrapy中如果没有请求传…

Hibernate(五)一对一单向关联映射

上次的博文中 Hibernate从入门到精通&#xff08;四&#xff09;基本映射我们已经讲解了一下基本映射和相关概念&#xff0c;接下来 我们会讲稍微复杂点的映射——关系映射。 关系映射分类 关系映射即在基本映射的基础上处理 多个相关对象和多个相关表之间联系的映射。关系映射…

爬虫_10_selenium在scrapy中使用分布式增量式

10_selenium在scrapy中使用&分布式&增量式 selenium在scrapy中的使用 https://news.163.com/ 爬取网易新闻中的国内、国际、军事、航空、无人机这五个板块下所有的新闻数据&#xff08;标题内容&#xff09; 分析 首页没有动态加载的数据 爬取五个板块对应的url 每一…