本帖最后由 lntuer 于 2020-1-20 16:10 编辑
一、目标网站
https://www.zhihuishu.com/
二、遇到难题
三、登陆代码
[Python] 纯文本查看 复制代码 from selenium import webdriver
import time
import re
import random
#输入账号
username=""
#输入密码
passwd=""
login_url='https://www.zhihuishu.com/'
option = webdriver.ChromeOptions()
option.add_argument('--mute-audio')
option.add_experimental_option('excludeSwitches', ['enable-automation'])
browser = webdriver.Chrome(chrome_options=option)
# browser = webdriver.Firefox()
browser.maximize_window()
browser.get(login_url)
browser.implicitly_wait(10)
elem=browser.find_element_by_xpath('//*[@id="header-top"]/div/ul/div/div[1]/a[1]')
elem.click()
time.sleep(3)
username_js='document.getElementById("lUsername").value="{}"'.format(username)
browser.execute_script(username_js)
time.sleep(2)
password_js='document.getElementById("lPassword").value="{}"'.format(passwd)
browser.execute_script(password_js)
time.sleep(2)
login_js='formSignUp();'
browser.execute_script(login_js)
time.sleep(3)
#studentIndex
current_url=browser.current_url
if "studentIndex" in current_url:
print("登陆成功啦……")
else:
i=0
while True:
print("尝试第{}次登陆……".format(i))
browser.refresh()
time.sleep(1)
username_js = 'document.getElementById("lUsername").value="{}"'.format(username)
browser.execute_script(username_js)
time.sleep(1)
password_js = 'document.getElementById("lPassword").value="{}"'.format(passwd)
browser.execute_script(password_js)
time.sleep(1)
login_js = 'formSignUp();'
browser.execute_script(login_js)
time.sleep(1)
current_url = browser.current_url
print(current_url)
if "studentIndex" in current_url or "entry" in current_url:
break
i+=1
大神勿喷,小白学习中…… |