• 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()
  • 相关阅读:
    xls与csv文件的区别
    青音,经典爱情语录
    win7用户账户自动登录方法汇总
    How to using Procedure found Lead Blocker
    FTS(3) BSD 库函数手册 遍历文件夹(二)
    FTS(3) BSD 库函数手册 遍历文件夹(一)
    DisplayMetrics类 获取手机显示屏的基本信息 包括尺寸、密度、字体缩放等信息
    About App Distribution 关于应用发布
    FTS(3) 遍历文件夹实例
    OpenCV 2.1.0 with Visual Studio 2008
  • 原文地址:https://www.cnblogs.com/zhangheng1/p/8649719.html
Copyright © 2020-2023  润新知