import time
from selenium import webdriver
from selenium.webdriver.common.by import By
import requests
if __name__ == "__main__":
# 由于selenium是通过driver去操作浏览器的,所以我们需要对应浏览器的driver对象
driver = webdriver.Chrome()
# 打开百度首页
driver.get("http://127.0.0.1:8001/index.php/index/user/login2.html")
# 找到用户名和密码输入框,并输入登录信息
username_input = driver.find_element(By.ID,"account")
password_input = driver.find_element(By.ID,"password")
username_input.send_keys("demo")
password_input.send_keys("123456")
# 找到登录按钮,并点击
login_button = driver.find_element(By.CSS_SELECTOR, ".btn.btn-primary.btn-lg.btn-block")
login_button.click()
# 等待登录完成,可以根据页面特征进行判断
# 这里以页面标题为例
time.sleep(5)
if driver.title == "混凝土养护工程实时监测系统":
# 登录成功,获取 Cookie
cookies = driver.get_cookies()
print("Cookies:", cookies)
else:
print("Login failed")
# 操作浏览器或验证页面数据等
#进行采集
#http://127.0.0.1:8001/index/Hunningtujc/getdata
# 添加需要的 cookie
formatted_cookies = {cookie['name']: cookie['value'] for cookie in cookies}
# 发起 GET 请求,并添加 cookie
apiUrl = "http://127.0.0.1:8001/index/Hunningtujc/getdata"
response = requests.get(url=apiUrl, cookies=formatted_cookies)
print(response.text)
# 检查响应状态码
if response.status_code == 200:
# 打印响应内容
print(response.text)
else:
print("请求失败,状态码:", response.status_code)
# 关闭浏览器进程
driver.quit()
网友评论