模拟12306登录

超级鹰:

#!/usr/bin/env python

# coding:utf-8

import requests

from hashlib import md5

class Chaojiying_Client(object):

def __init__(self, username, password, soft_id):

self.username = username

password = password.encode('utf8')

self.password = md5(password).hexdigest()

self.soft_id = soft_id

self.base_params = {

'user': self.username,

'pass2': self.password,

'softid': self.soft_id,

}

self.headers = {

'Connection': 'Keep-Alive',

'User-Agent': 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)',

}

def PostPic(self, im, codetype):

"""

im: 图片字节

codetype: 题目类型 参考 http://www.chaojiying.com/price.html

"""

params = {

'codetype': codetype,

}

params.update(self.base_params)

files = {'userfile': ('ccc.jpg', im)}

r = requests.post('http://upload.chaojiying.net/Upload/Processing.php', data=params, files=files, headers=self.headers)

return r.json()

def ReportError(self, im_id):

"""

im_id:报错题目的图片ID

"""

params = {

'id': im_id,

}

params.update(self.base_params)

r = requests.post('http://upload.chaojiying.net/Upload/ReportError.php', data=params, headers=self.headers)

return r.json()

def codecjy():

chaojiying = Chaojiying_Client('xxxx', 'xxxxxxx', '902590')#用户中心>>软件ID 生成一个替换 96001用户名 密码 软件id

im = open('./code.png', 'rb').read()#本地图片文件路径 来替换 a.jpg 有时WIN系统须要//

code=chaojiying.PostPic(im,9004)['pic_str']#1902 验证码类型 官方网站>>价格体系 3.4+版 print 后要加()

return code

爬虫程序:

from selenium import webdriver

from selenium.webdriver import ActionChains

from time import sleep

from PIL import Image #安装PIL或者是Pillow

from CJY import Chaojiying_Client

#封装一个识别验证码的函数

def transformCode(imgPath,imgType):

chaojiying = Chaojiying_Client('超级鹰用户名', '超级鹰密码', '899370')

im = open(imgPath, 'rb').read()

return chaojiying.PostPic(im, imgType)['pic_str']

bro = webdriver.Chrome(executable_path='./chromedriver.exe')

bro.get('https://kyfw.12306.cn/otn/login/init')

sleep(2)

#将当前浏览器页面进行图片保存

bro.save_screenshot('./main.png')

#将验证码的局部区域进行裁剪

#捕获标签在页面中的位置信息

img_tag = bro.find_element_by_xpath('//*[@id="loginForm"]/div/ul[2]/li[4]/div/div/div[3]/img')

location = img_tag.location#标签的起始位置坐标(左下角坐标)

size = img_tag.size#标签的尺寸

#裁剪范围对应的矩形区域

rangle = (int(location['x']),int(location['y']),int(location['x']+size['width']),int(location['y']+size['height']))

#使用Image工具进行指定区域的裁剪

i = Image.open('./main.png')

frame = i.crop(rangle)#crop就是根据指定的裁剪范围进行图片的截取

frame.save('code.png')

#调用打码平台进行验证码的识别

result = transformCode('./code.png',9004)

print(result) #x1,y1|x2,y2|x3,y3

#x1,y1|x2,y2|x3,y3 ==>[[x1,y1],[x2,y2],[x3,y3]]

all_list = []#[[x1,y1],[x2,y2],[x3,y3]] 验证码中图片中的点击内容的坐标

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)

for point in all_list:

x = point[0]

y = point[1]

ActionChains(bro).move_to_element_with_offset(img_tag,x,y).click().perform()

sleep(1)

bro.find_element_by_id('username').send_keys('xxxxxx') #12306用户名

sleep(1)

bro.find_element_by_id('password').send_keys('xxxx') #密码

sleep(1)

bro.find_element_by_id('loginSub').click() #点击事件,登录

sleep(10)

print(bro.page_source)

bro.quit()

python 12306登录_python爬虫--模拟12306登录相关推荐

  1. python爬虫登录微博_python爬虫-模拟微博登录

    微博模拟登录 这是本次爬取的网址:https://weibo.com/ 一.请求分析 找到登录的位置,填写用户名密码进行登录操作 看看这次请求响应的数据是什么 这是响应得到的数据,保存下来 exect ...

  2. python爬虫登录微博_python爬虫-模拟微博登录功能

    微博模拟登录 这是本次爬取的网址:https://weibo.com/ 一.请求分析 找到登录的位置,填写用户名密码进行登录操作 看看这次请求响应的数据是什么 这是响应得到的数据,保存下来 exect ...

  3. python 12306登录 2019_python爬虫--模拟12306登录

    模拟12306登录 超级鹰: #!/usr/bin/env python # coding:utf-8 import requests from hashlib import md5 class Ch ...

  4. 使用python爬虫模拟12306登陆

    使用python爬虫模拟12306登陆   需要用到requests库函数,下载requests库函数地方法:直接在命令行输入以下代码就可以了就可以了. pip install requests 模拟 ...

  5. python爬虫淘宝登录_python爬虫实现模拟淘宝登录

    就算所有人都不支持你.这条路会很曲折,你也会一度认为是不是自己选错了,但只要坚持,就算最后没有成功,但努力了就不会有遗憾. python爬虫模拟淘宝登录 小贴士 这个爬虫和搜索引擎的爬虫是有区别的.搜 ...

  6. python爬虫怎么登录_Python爬虫基础-模拟登陆

    为什么我们要让爬虫模拟登陆呢?有些内容只有登陆才能进行爬取,如知乎,不登录的主页只能看到注册和登陆 : 你想爬取自己的个人信息 有什么方法呢?cookie 在互联网发展的早期,由于大家的服务器都不是太 ...

  7. python成绩查询系统_Python爬虫实战:登录教务系统查成绩

    本文记录我用Python登录教务系统查询成绩的过程.手动输入验证码,简单获取成绩页面.后续将可能更新自动识别验证码登录查询 前期准备 本爬虫用到了Python的Requests库和BeautifulS ...

  8. python脚本自动qq签到_Python爬虫实现自动登录、签到功能 附代码

    这篇文章主要介绍了Python爬虫实现自动登录.签到功能的代码,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下 前几天在一个素材网站上下载东 ...

  9. python爬带用户名密码的网页_python爬虫用户名密码登录POST

    # -*- coding: utf-8 -*- """ Created on Wed Jun 6 13:18:58 2018 @author: Lenovo " ...

最新文章

  1. Mybatis使用时因jdbcType类型大小写书写不规范导致的异常
  2. JavaI/O:使用InputStreamReader和BufferedReade实现控制器输入输出
  3. mongodb 压缩——3.0+支持zlib和snappy
  4. 【 Laravel 】日 常 整 理 记 录 分 享
  5. SAP Kyma组件一览
  6. JavaEE概念简介
  7. 电脑显示链接网络正常但是不能上网,用某些软件会提示系统丢失libeay32.dll
  8. Python解决print()不换行问题
  9. C#基础知识之图解TCP IP》读书笔记
  10. 关于三极管的饱和与截止(失真)
  11. Hello,programer! 我的第一篇博客
  12. 如何制定有效的项目章程?【含项目章程模板】
  13. eleme后台的el-form表单分为左右两边放数据 :inline=true
  14. Java杂项基础知识点总结
  15. AMD Software Adrenalin Edition 23.5.1驱动发布,快速获取驱动
  16. 综合练习Java算法
  17. 斐波那契数列 - 实现一个斐波那契数列,并且打印出来
  18. 优秀求职者应主动问的5个问题
  19. tp-link wr 841n 300m 局域网限速的研究
  20. Android怎么自定义充电铃声,安卓手机怎么自定义充电提示音_自定义安卓充电提示音教程_3DM手游...

热门文章

  1. mysql备份工具xtr,xtrbackup备份mysql与恢复
  2. mxnitro浏览器 v1.0.0.500 官方版
  3. 微信号第二次修改技巧?官方终于支持修改!
  4. DBTC——流动性、使用人数自我驱动增长的分叉币
  5. Centos7安装和配置Mysql5.7_CentOS7安装和配置MySQL5.7
  6. 云呐|智能化运维平台,信息化系统智能运维平台
  7. 上海浦东新区计算机学校排名2015,2015年上海市浦东新区初级中学最新排名.pdf
  8. 智慧工地AI视频分析系统 opencv
  9. Tio-Android简单使用篇
  10. 深圳户籍居民如何参加社保