selenium登录后请求接口带cookie

```
#coding=utf-8
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
import time
import requests
print(webdriver.__version__)
#driver = webdriver.Chrome() # 或者其他的 WebDriver
# 打开自定义配置
options = webdriver.ChromeOptions()
# 设置浏览器不会关闭
options.add_experimental_option('detach', True)
# 禁用浏览器扩展
options.add_argument('--disable-extensions')
# 禁用浏览器弹窗
options.add_argument('--disable-popup-blocking')
# 设置浏览器UA
options.add_argument('--user-agent=Mozilla/5.0(windows NT 10.0; Win64; X64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36')
driver = webdriver.Chrome(options=options)
driver.maximize_window()
driver.get("https://186.318.205.123/wui/index.html#/?_key=4fwum5")
time.sleep(10)
username_input = driver.find_element(By.ID,"loginid")
username_password = driver.find_element(By.ID,"userpassword")
password_input = driver.find_element(By.ID,"validatecode")
username_input.send_keys("test")
time.sleep(1)
username_password.send_keys("123456")
time.sleep(10)
#password_input.send_keys("123456")
driver.find_element(By.ID, "submit").send_keys(Keys.ENTER)
# 登录成功,获取 Cookie
time.sleep(5)
cookies = driver.get_cookies()
#print("Cookies:", cookies)
# 添加需要的 cookie
formatted_cookies = {cookie['name']: cookie['value'] for cookie in cookies}
# 关闭浏览器
#driver.quit()
print('===============================')
print(formatted_cookies)
print('===============================')
# 发起 GET 请求,并添加 cookie
apiUrl = 'http://186.318.205.123/api/kq/myattendance/getHrmKQMonthReportInfo?typevalue=2024-10&loaddata=1&type=2' # 替换为目标 POST 请求的 URL
post_data = {
'key1': 'value1',
'key2': 'value2'
}
response = requests.post(apiUrl, data=post_data,cookies=formatted_cookies)
# 检查响应状态码
print(response.status_code)
if response.status_code == 200:
# 打印响应内容
print(response.text)
else:
print("请求失败,状态码:", response.status_code)
apiUrl = 'http://186.318.205.123/api/hrm/search/getHrmSearchResult' # 替换为目标 POST 请求的 URL
post_data = {
'tabkey' : 'default_3',
'showAllLevel' :1
}
response = requests.post(apiUrl, data=post_data,cookies=formatted_cookies)
# 检查响应状态码
print(response.status_code)
if response.status_code == 200:
# 打印响应内容
print(response.text)
else:
print("请求失败,状态码:", response.status_code)

try:
json_data = response.json()
print("JSON 数据:", json_data)
print(json_data['sessionkey'])
except ValueError:
print("响应不是 JSON 格式")
apiUrl = 'http://186.318.205.123/api/ec/dev/table/datas?dataKey='+json_data['sessionkey']+'¤t=1&sortParams=%5B%5D&' # 替换为目标 POST 请求的 URL
post_data = {
'dataKey' : json_data['sessionkey'],
'current' : 1,
'sortParams':''
}
response = requests.post(apiUrl, data=post_data,cookies=formatted_cookies)
print(response.status_code)
if response.status_code == 200:
# 打印响应内容
print(response.text)
else:
print("请求失败,状态码:", response.status_code)
```

    A+
发布日期:2024年11月01日  所属分类:未分类

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: