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()
python+selenium模拟登录后调用接口
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 前言 本文的文字及图片来源于网络,仅供学习、交流使用,不具有任何商业用途,如有问题请及时联系我们以作处理。 PS:...
- 本次使用BeautifulSoup查找验证码tag信息,redis查找验证码,然后使用selenium登录 使用s...
- 以百度搜索为例,利用类调用实例的方法编写脚本。BaiduSearch.py 报错: 导致出错的根源就是编码问题。脚...