Javascript 使用Python获取DHCP客户端列表

Javascript 使用Python获取DHCP客户端列表,javascript,python,html,login,python-requests,Javascript,Python,Html,Login,Python Requests,我目前正在编写一个python脚本,该脚本要求我获取本地路由器DHCP服务器上当前客户端的列表。我的第一个想法是如何获取包含列表的网页(http://192.168.2.1/lan_dhcp.stm)然后解析数据并使用它 问题是,有时路由器的web界面会要求我登录,即使没有设置密码,您只需单击“提交”。如何发送登录请求(http://192.168.2.1/login.htm)以保证我能够访问客户端列表?有没有更好的办法? login.cgi: <html><head>&

我目前正在编写一个python脚本,该脚本要求我获取本地路由器DHCP服务器上当前客户端的列表。我的第一个想法是如何获取包含列表的网页(
http://192.168.2.1/lan_dhcp.stm
)然后解析数据并使用它

问题是,有时路由器的web界面会要求我登录,即使没有设置密码,您只需单击“提交”。如何发送登录请求(
http://192.168.2.1/login.htm
)以保证我能够访问客户端列表?有没有更好的办法?
login.cgi

<html><head><META http-equiv="Content-Type" content="text/html; charset=UTF-8" ></head><script>
                                      function reload(){                                      top.topFrame.location.href=top.topFrame.location.href;                                      document.location.href="lan_dhcp.stm";}
setTimeout("reload()", 900);
</script></html>

看起来浏览器中正在执行一些客户端javascript代码-查看您的
pws_temp
密码值如何转换为
pws
加密值:


请求
不是浏览器,也没有内置的javascript引擎。

我会切换到真正的浏览器自动化。除了常规的普通浏览器,如Firefox或Chrome,您还可以控制无头浏览器,如

下面是完整的登录代码,让您开始。请注意,根据页面来源,登录表单本身位于
iframe
中-您必须切换到它的上下文才能找到表单的元素:

from selenium import webdriver

password = "My password"

driver = webdriver.PhantomJS()
driver.get("http://192.168.2.1/login.htm")

# switching to the main frame on the page    
driver.switch_to.frame("mainFrame")

password = driver.find_element_by_name("pws_temp")
password.send_keys(password)
password.submit()  # this would find the corresponding form and submit it

# or you can also submit the form this way: password.send_keys(password + "\n")

嘿@alecxe,很抱歉回答晚了。我试着使用你概述的方法,但我得到了以下结果。“我做错了什么?”BernardMeurer可能有多种原因。你能不能用一个完整的HTML页面和登录表做一个要点?谢谢@亚历克斯,好的。我通过使用
wget
在Chrome上使用F12视图获得了所有可以看到的文件。它们如下:,@BernardMeurer谢谢!当打开登录表单时,请右键单击页面上的任意位置,选择查看页面源代码,将看到的HTML复制到要点中。嗯,如果没有设置密码,它会工作,但是如果设置了任何密码,它不会工作。这是密码,我最好不要密码。我还尝试使用selenium获取dhcp列表页面(使用相同的driver.get方法),如何将驱动程序获取的内容打印到终端屏幕?
from selenium import webdriver

password = "My password"

driver = webdriver.PhantomJS()
driver.get("http://192.168.2.1/login.htm")

# switching to the main frame on the page    
driver.switch_to.frame("mainFrame")

password = driver.find_element_by_name("pws_temp")
password.send_keys(password)
password.submit()  # this would find the corresponding form and submit it

# or you can also submit the form this way: password.send_keys(password + "\n")