selenium+ChromeOptions登录抓取北京市社保卡信息

#coding:utf-8

from PIL import Image
from selenium import webdriver
import random
import time
import ydm

class Login:

def __init__(self):
option = webdriver.ChromeOptions()
option.add_argument('--start-maximized') # 最大化
option.add_argument('--headless') # 无界面
self.driver = webdriver.Chrome(chrome_options=option)
self.url = 'http://www.bjrbj.gov.cn/csibiz/indinfo/login.jsp'

def t_login(self):
dr = self.driver
dr.get(self.url)
time.sleep(random.uniform(1, 3))
dr.find_element_by_xpath('//*[@id="i_username"]').send_keys('用户账号')
dr.find_element_by_xpath('//*[@id="i_password"]').send_keys('用户密码')
dr.get_screenshot_as_file('q.jpg')
imgs = dr.find_element_by_xpath('//*[@id="indsafecode"]')
location = imgs.location # 坐标: {'y': 19, 'x': 498}
size = imgs.size # 尺寸: {'width': 500, 'height': 22}
rangle = (
int(location['x']), int(location['y']), int(location['x'] + size['width']),
int(location['y'] + size['height']))
img = Image.open('q.jpg')
im = img.crop(rangle) # 裁剪图片
im.save('q_img.png')
username = '云打码账号'
password = '密码'
appid = 4359
appkey = '0715011dad66b60956de51723d5509c2'
filename = 'q_img.png'
codetype = 1004
timeout = 60
if (username == 'username'):
print('请设置好相关参数再测试')
else:
yundama = ydm.YDMHttp(username, password, appid, appkey)
balance = yundama.balance()
print('balance: %s' % balance)
cid, result = yundama.decode(filename, codetype, timeout)
code = result
print(code)
dr.find_element_by_xpath('//*[@id="i_safecode"]').send_keys(code)
dr.find_element_by_xpath('//*[@id="shoujiyanzhengma"]').click()
time.sleep(random.uniform(2, 3))
dr.switch_to_alert().accept() #点击alert弹窗
tel_message = input('请输入短信验证码:')
time.sleep(random.uniform(3, 5))
dr.find_element_by_xpath('//*[@id="indform"]/div[4]/input[1]').send_keys(tel_message)
time.sleep(2)
dr.find_element_by_xpath('//*[@id="indform"]/div[5]/input').click()
time.sleep(random.uniform(2, 3))
print(dr.get_cookies())
cookie = [item["name"] + "=" + item["value"] for item in dr.get_cookies()]
cookiestr = ';'.join(item for item in cookie)
# print(cookiestr)
with open('cookie.txt', 'w') as f:
f.write(cookiestr)
f.close()
dr.quit()


l = Login()
l.t_login()
posted on 2018-03-26 11:21  一止小菜鸟  阅读(214)  评论(0编辑  收藏  举报