前言
使用python3.6 selenium包实现用户密码自动登录企查查
编写python函数通过用户名、密码入参返回登录cookie
编写函数
编写get_cookie函数
注意安装chromedriver.exe以及指定chromedriver.exe路径
# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.chrome.options import Options
from selenium.webdriver import ActionChains
import time
import xlwt
import sys
from tools.tools import *
def get_cookie(username, passwd):
import importlib # if python 3.*
importlib.reload(sys) # if python 3.*
# 伪装成浏览器,防止被识破
option = webdriver.ChromeOptions()
# option.add_argument('--headless')
# option.add_argument('--disable-gpu')
option.add_argument(
'--user-agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
'Chrome/65.0.3325.146 Safari/537.36"')
driver = webdriver.Chrome(chrome_options=option,
executable_path='C:\Program Files (x86)\Google\Chrome\App\chromedriver.exe')
driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
"source": """
Object.defineProperty(navigator, 'webdriver', {
get: () => undefined
})
"""
})
# 打开登录页面
driver.get('https://www.qcc.com/user_login')
# driver.maximize_window()
# 单击用户名密码登录的标签
tag = driver.find_element_by_xpath('//*[@id="normalLogin"]')
tag.click()
# 将用户名、密码注入
driver.find_element_by_id('nameNormal').send_keys(username)
driver.find_element_by_id('pwdNormal').send_keys(passwd)
time.sleep(2)
# chrome_options = Options()
# 开始滑动
distance = 308
track = get_track(distance)
# 按住左键不放
# action.click_and_hold(source).perform()
# for i in track:
# action.move_by_offset(xoffset=i, yoffset=0).perform()
# action.reset_actions()
i = 0
while True:
source = driver.find_element_by_xpath('//*[@id="nc_1_n1z"]')
action = ActionChains(driver)
action.reset_actions()
action.drag_and_drop_by_offset(source, 308, 0)
# 释放鼠标
action.release().perform()
time.sleep(2)
tip = driver.find_element_by_xpath('//*[@id="dom_id_one"]/div')
print(tip.text + str(i) + "\n")
if "验证通过" in tip.text:
break
elif i > 10:
driver.close()
return "error"
else:
driver.find_element_by_xpath('//*[@href="javascript:noCaptcha.reset(1)"]').click()
i += 1
time.sleep(2) # 休眠,人工完成验证步骤,等待程序单击“登录”
# 单击登录按钮
btn = driver.find_element_by_xpath('//*[@id="user_login_normal"]/button')
btn.click()
cookies = driver.get_cookies()
cookieStr = ""
for item in cookies:
cookieStr += item["name"] + "=" + item["value"] + "; "
driver.close()
print("获取cookie成功\n{0}".format(cookieStr))
return cookieStr
附 tools.py工具函数
在get_cookie中引用get_track(distance)自动滑动验证
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver import ActionChains
def get_track(distance):
track = []
current = 0
mid = distance * 3 / 4
t = 0.2
v = 0
while current < distance:
if current < mid:
a = 2
else:
a = -3
v0 = v
v = v0 + a * t
move = v0 * t + 1 / 2 * a * t * t
current += move
track.append(round(move))
return track
# 滑动验证码识别
def slide_discern():
print("滑块验证码验证中。。。") # < br > 创建无界面模式
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
driver = webdriver.Chrome(chrome_options=chrome_options) # < br > # 获取到需滑动的按钮
source = driver.find_element_by_xpath('//*[@id="nc_1_n1z"]')
action = ActionChains(driver)
# 按住左键不放
action.click_and_hold(source).perform()
# 开始滑动
distance = 34 # < br > # 模拟以人为速度拖动
track = get_track(distance)
for i in track:
action.move_by_offset(xoffset=i, yoffset=0).perform()
action.reset_actions()
# 释放鼠标
action.release().perform()
使用
if __name__ == "__main__":
username = "1"
passwd = "1"
cook = get_cookie(username, passwd)
if cook == "error":
print("success")
else:
print("failed")
总结
获取企查查登录cookie后,直接通过http请求就能够进行后续想要做的操作了。