具体事项就看我这两个帖子 我就直接附上主代码了
https://www.52pojie.cn/thread-1462721-1-1.html
https://www.52pojie.cn/thread-1455857-1-1.html
说简单也不简单,说难也不难。反正花了我大把的时间在selenium的配置上面。
[Python] 纯文本查看 复制代码 chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument("--no-sandbox")
browser = webdriver.Chrome(executable_path=r"your driver", chrome_options=chrome_options)
# browser = webdriver.Chrome()
try:
# max window, because we need to slider captcha. get position
browser.maximize_window()
except:
pass
browser.get("https://www.zhihu.com/signin")
browser.find_element_by_css_selector("div[class='SignFlow-tab']").click()
browser.find_element_by_css_selector(".SignFlow-accountInput.Input-wrapper").send_keys("account")
browser.find_element_by_css_selector(".SignFlow-password input").send_keys("password")
browser.find_element_by_css_selector(".Button.SignFlow-submitButton").click()
time.sleep(3)
# may login failed
login_success = False
# See the configuration in the links above
chaojiying = Chaojiying_Client('xxxxx', 'xxx', 'xxxx')
# get verify code
img_href = browser.find_element_by_css_selector(".yidun_bg-img").get_attribute("src")
slider = browser.find_element_by_xpath("/html/body/div[4]/div[2]/div/div/div[2]/div/div[2]/div[2]")
# get picture of gap ==> need ChaoJiYing
img = urllib.request.urlretrieve(img_href, "code.jpg")
im = open("code.jpg", "rb").read()
dic = chaojiying.PostPic(im, 9101)
# 从返回的值里面找到需要点击的下标
# href = img.get_attribute("href")
# 拿到验证码 上传到打码平台去识别,然后去滑动,登录成功 ok 美滋滋.
# 从返回的值里面找到需要点击的下标
res = dic['pic_str']
print(res)
# 分割 这里大家可以自己输出一下 就知道了
# 28,110
temp = res.split(",")
# adjust data ==> CJY return wrong dates,we should adjust it. According to official documents
# 9101 坐标选一,返回格式:x,y 15
# x = int(temp[0]) - 10
x = int(temp[0]) - 15
y = int(temp[1])
print("x is {}, y is {}".format(x, y))
# 移动到某个区域 然后再进行点击
# 然后找到区域 进行偏移
# 是事件链
# print("接下来的事件")
# 按住不释放 ==> 拖动
ActionChains(browser).click_and_hold(slider).perform()
# only xoffset
ActionChains(browser).move_by_offset(xoffset=x, yoffset=0).perform()
time.sleep(2)
# 释放 即可登录成功
ActionChains(browser).release().perform()
time.sleep(10)
# while not login_success:
# try:
# notify_ele = browser.find_element_by_css_selector(".AppHeader-userInfo")
# login_success = True
# except:
# pass
browser.close()
|