Cause: Every day boot must log in the campus network, although the program is very simple, but as a contemporary college students should learn how to be more efficient lazy.
Background: a university student in Chongqing, using the campus network provided by China Telecom (smoothie telecom dead pit is also expensive), Google browser (Chrome cowhide)
So let’s do:
1. Analyze the normal login process of the campus account:
-
Log in the campus wifi network — open the browser and enter the IP (it may automatically pop up) — enter the account, password, select the identity type
We can script the browser to simulate the user’s login. Driver = webdriver. Chrome () # here using Chrome, driver, get (‘ http://10.255.255.222/a70.htm ‘) # type your network login url here
2. Enter the account, password, and user type
Universal F12 call out front-end code
-
Find the input user type by ISP_select
Input_tag1 = driver.find_element_by_name(‘ISP_select’) # Choose your broadband carrier
And look for the username and password
Find the username and password
Input_tag = driver.find_element_by_xpath("//form[@name='f3']/input[@name =' DDDDD']") # Input_tag2 = driver.find_element_by_xpath("//form[@name='f3']/input[@name = Input_tag2. send_keys("222222222"Copy the code
3. Check the network connectivity
-
Sometimes when other device login accounts are running this script, the screen will return and you will need to log in again.
Therefore, we need to add a connection code to determine whether the network is connected, and if not, run the connection code againdef Ping(): backinfo = subprocess.call(‘ping www.baidu.com -n 1’, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, Stderr =subprocess.PIPE) if backinfo: print(‘ network is not connected ‘) return 1 else: print(” network is connected “) return 2
conclusion
Overall code:
from selenium import webdriver from selenium.webdriver.common.keys import Keys import time import subprocess def spider(): Sleep (2) input_tag1 = webdriver.chrome () driver.get('http://baidu.com.htm' Driver.find_element_by_name ('ISP_select') # find the combo box, this can be found directly by name input_tag1. Send_keys (' telecom user login ') # To select your broadband carrier input_tag = driver.find_element_by_xpath("//form[@name='f3']/input[@name =' DDDDD']") # Input_tag2 = driver.find_element_by_xpath("//form[@name='f3']/input[@name = Input_tag2. send_keys("2222222") # ENTER the password input_tag2.send_keys(keys.enter) # Def Ping(): backinfo = subprocess.call('ping www.baidu.com -n 1', shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, Stderr =subprocess.PIPE) if backinfo: print(' network not connected ') return 1 else: print(" network ") return 2 if __name__ == '__main__': While True: spider() print() connection = Ping() if connection == 2: print("ping") elif connection == 1: spider() exit()Copy the code
There was a bit of a head-scratcher in the process, which was that the browser couldn’t be called
The process of code writing has also referred to the articles of other big names in CSDN. Since the process of writing has not been deliberately collected, there is no famous reference here, but thank you very much
Recently, many friends have sent messages to ask about learning Python. For easy communication, click on blue to join yourselfDiscussion solution resource base