我有有线互联网,需要我通过网页登录。每天早上 8 点和 12 点都会重置,这很烦人。我想编写一个 python 脚本来自动执行登录过程。到目前为止,我已经阅读了很多 StackOverflow 解决方案,但没有任何效果。我已经尝试过Requests , Twill , RoboBrowser等等
在检查页面源代码时,我发现了一个 doLogin() ajax 脚本,该脚本由登录按钮触发。这是完整的page source .
以下是我失败的实现之一
import requests
# Fill in your details here to be posted to the login form.
payload = {
'action': 'http://10.10.0.1/login',
'actualusername': 'username',
'actualpassword': 'password'
}
# Use 'with' to ensure the session context is closed after use.
with requests.Session() as s:
p = s.post("http://103.251.83.134/captiveportal/Default.aspx", data=payload)
# print the html returned or something more intelligent to see if it's a successful login page.
print p.text
# An authorised request.
#r = s.get('http://www.google.com')
#print r.text
编辑:解决方案
我使用 Selenium WebDriver 来解决这个问题。检查答案。
最佳答案
使用Selenium :) 将ChromeDriver下载到路径中,创建一个两次时间变量并每分钟检查一次时间。如果是“登录时间”,您的浏览器将通过授权。
from selenium import webdriver
def Authorization_for_broadband():
driver = webdriver.Chrome("C:\YOURPATHTO\CHROMEDRIVER.EXE")
driver.get('http://10.10.0.1/login')
driver.find_element_by_xpath('//*[@id="username"]').send_keys('USERNAME')
driver.find_element_by_xpath('//*[@id="password"]').send_keys('PASSWORD')
driver.find_element_by_xpath('//*[@id="btnLogin"]').click()
driver.close
while(1):
if time=='your-login-period1' or time == 'your-login-period2':
Authorization_for_broadband()
关于javascript - 使用 Python 自动登录我的本地互联网页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43172514/