【问题标题】:Python script to click Enable / disable WiFi单击启用/禁用 WiFi 的 Python 脚本
【发布时间】:2018-10-27 18:01:08
【问题描述】:

我想创建一个简单的 python 脚本,它将登录到我的路由器网站,关闭 wifi 然后应用更改并执行相反的操作(打开 wifi 并应用更改),不幸的是我对 html 标记的了解而且python还不够好,所以请帮助我:)

这是我目前创建的:

from bs4 import BeautifulSoup as soup
from urllib.request import urlopen as uReq
import requests

my_url = 'http://router/WLG_adv.htm'
response = requests.get(my_url, auth=requests.auth.HTTPBasicAuth('My_username', 'My_password'))
c = response.content
uClient = uReq(my_url)
soup = BeautifulSoup(c)
rows = soup.findAll('tr')[4::5]
soup.find("button", "button-apply")

...感谢我能够使用凭据登录到正确的页面并获得正确的代码来操作:

这是“wifi 启用复选框部分”(我相信它是 check_wifi_sche())

[<tr>
<td colspan="2">
<input checked="" name="enable_ap" onclick="check_wifi_sche()" type="checkbox" value="enable_ap"/> Enable Wireless Router Radio </td></tr>,   <tr>
<td colspan="2">
<div id="wifi_sche_div1">
<input name="wifi_onoff" type="checkbox" value="wifi_onoff"/> Turn off wireless signal by schedule 
</div>
</td>
</tr>, <tr><td colspan="2" height="12"><div style="background-image:url('liteblue.gif');width:100%"> </div></td></tr>, <tr>
<td colspan="2">
<input checked="" name="wsc_config" type="checkbox"/>
<font>  Keep Existing Wireless Settings  </font>
</td>
</tr>]

这里是“应用按钮部分”:

>>> soup.find("button", "button-apply")

<button class="button-apply" name="Apply" onclick="buttonClick(this,'Apply');return checkData();" type="submit" value="Apply"> <span class="roundleft_apply">Apply <span class="apply-icon">    </span></span><span class="roundright_apply">   </span></button>

提前致谢。

【问题讨论】:

  • 使用 selenium,它是你想要做的事情的合适工具

标签: python html beautifulsoup python-requests urllib


【解决方案1】:

我终于设法解决了这个问题。感谢您的提示 Dev,这真的很有帮助。 所以我的任务是编写简单的脚本来启用/禁用 Wifi,然后单击路由器 NETGEAR WNR3500Lv2 的应用按钮。

如果有人需要,我会发布信息如何做:) (请注意,在路由器地址行中(您必须将您的路由器凭据放在我放置 X 和 Y 的位置。

# -*- coding: latin-1 -*-                   
# Ranorex selocity do chrome'a               <<< Very helpful tool for chrome to extract paths from websites.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome()   # Remember to have your chrome driver in $PATH (download driver from google)
base_url = "http://XXXXXXXXXXXX:YYYYYYYYYY@ROUTERS-IP-ADDRESS/WLG_adv.htm"            # Place your login:password here:
driver.get(base_url)

# Wifi click
wifi_button = driver.find_elements_by_xpath("//input[@name='enable_ap' and @value='enable_ap']")[0]
wifi_button.click()

# Apply click
apply_button = driver.find_element_by_xpath("//form[@id='target']/table[@class='subhead2-table']//button[@name='Apply']/span[@class='roundleft_apply']")
apply_button.click()

# Closing page (don't worry, it will complete the task, even when it's closed fast.
driver.stop_client()
driver.close()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多