1、模擬入口:https://passport.zhihuishu.com/login?service=https://onlineservice.zhihuishu.com/login/gologin
2、登錄完成之后,get請求訪問 'https://onlineservice.zhihuishu.com/login/getLoginUserInfo'這個介面
成功:只回傳一個status=1
失敗:回傳403代表,模擬失敗,同時它回傳status=1,然后會帶一個token
要求:python語言,requests/selenium實作模擬登錄
uj5u.com熱心網友回復:
寫了一個模擬登陸牛客網,然后抓題面的代碼,用python+selenium,可以參考
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
from bs4 import BeautifulSoup
def bsNCProblem():
chrome_driver_path = '''E:\chromedriver.exe'''
driver = webdriver.Chrome(executable_path=chrome_driver_path)
ncusername, ncpwd = '***', '***'
url = 'https://ac.nowcoder.com/acm/contest/950/K'
driver.get(url)
try:
username_element = WebDriverWait(driver,10).until(
EC.presence_of_element_located((By.ID,"jsEmailIpt"))
)
pwd_element = driver.find_element_by_id('jsPasswordIpt')
loginbtn_element = driver.find_element_by_id('jsLoginBtn')
username_element.send_keys(ncusername)
pwd_element.send_keys(ncpwd)
loginbtn_element.click()
driver.refresh()
time.sleep(3)
title_element = driver.find_element(By.CLASS_NAME,'terminal-topic-title')
limit_element = driver.find_element(By.CLASS_NAME,'subject-item-wrap')
describe_element = driver.find_element(By.CLASS_NAME,'subject-describe')
print(title_element.text)
print(limit_element.text)
print(describe_element.text)
driver.close()
finally:
pass
if __name__ == "__main__":
bsNCProblem()
uj5u.com熱心網友回復:
# -*- coding:utf-8 -*-import json
import requests
import time
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.common.action_chains import ActionChains
if __name__ == '__main__':
dcap = dict(DesiredCapabilities.PHANTOMJS)
dcap["phantomjs.page.settings.userAgent"] = (
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36"
)
# browser = webdriver.Chrome(executable_path="/Users/houduan/chromedriver")
# browser = webdriver.PhantomJS(executable_path='')
browser = webdriver.Firefox(executable_path="/Users/houduan/geckodriver")
url = 'https://passport.zhihuishu.com/login?service=https://onlineservice.zhihuishu.com/login/gologin'
browser.get(url)
time.sleep(1)
# 登錄和機器配置資訊
username = "130xxxx7316" # 帳號
passwd = "xxxxx" # 密碼
ActionChains(browser).move_by_offset(600, 0).perform()
elem = browser.find_element_by_id("lUsername")
elem.send_keys(username)
ActionChains(browser).move_by_offset(300, 0).perform()
elem = browser.find_element_by_id("lPassword")
elem.send_keys(passwd)
elem = browser.find_element_by_class_name("wall-sub-btn")
elem.click()
time.sleep(3)
browser.get('https://onlineservice.zhihuishu.com/login/getLoginUserInfo')
session = requests.session()
cookies=browser.get_cookies()
for item in cookies:
session.cookies.set(item['name'],item['value'])
headers = {
'Connection': 'keep-alive',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:73.0) Gecko/20100101 Firefox/73.0'
}
userinfo_url = 'http://onlineservice.zhihuishu.com/login/getLoginUserInfo'
userinfo_data = json.loads(session.get(userinfo_url
, headers=headers
).text)
print(userinfo_data)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/85391.html
