preface:
In recent years, the anti crawling of 12306 has become more and more serious. From obtaining tk parameters a year ago to now, JS, CSS and other encryption methods have been added!
At present, most people use selenium to log in, and this article is no exception.
Environmental Science:
Windows
** python 3.6.5**
modular:
selenium
** pyautogui**
time
Step 1:
Instantiate a browser and enter the 12306 official website
driver = webdriver.Chrome()driver.get('https://kyfw.12306.cn/otn/resources/login.html')driver.implicitly_wait(10)driver.maximize_window()
Step 2:
Click account login
driver.find_element_by_xpath('//*[@id="J-userName"]').send_keys('123456')driver.find_element_by_xpath('//*[@id="J-password"]').send_keys('123456')
Step 3:
In the input box, simulate the input of account and password:
driver.find_element_by_xpath('//*[@id="J-userName"]').send_keys('123456')driver.find_element_by_xpath('//*[@id="J-password"]').send_keys('123456')
Step 4:
When the account password is entered, what we should do is simulate clicking on the verification code picture! Finally, if your time is not very tight and you want to improve python quickly, the most important thing is not afraid of hardship. I suggest you can fight ♥ Xin (homonym): 2763177065, that's really good. Many people have made rapid progress. You need to be not afraid of hardship! You can add it and have a look~
Then first download the picture:
yzm_code = driver.find_element_by_xpath('/html/body/div[2]/div[2]/div[1]/div[2]/div[3]/div')yzm_code.screenshot('yzm.png')
Step 5:
After downloading the verification code, you can do whatever you want, whether it's docking and coding platform, your own training model, or using code to simulate clicking!
The first two methods can't be explained at all, so here we use the third method to simulate clicking.
Here we talk about the effect we need to achieve. There are a total of 8 pictures on the verification code, so the effect I want to achieve is that I enter 1,2,3, and the program automatically clicks on 1,2,3 pictures.
So we first get the coordinates of the center point of each picture. It's regular here. Smart people must understand it at a glance.
I wrote this casually:
code = input('Please enter the verification code:') time.sleep(5) point_map = { '1': '40,45', '2': '116,53', '4': '257,50', '5': '40,121', '6': '116,133', '3': '185,52', '7': '185,132', '8': '257,130'}
def get_point(indexs): indexs = indexs.split(',') temp = [] for index in indexs: temp.append(point_map[index]) print(temp) return temptemp = get_point(code)
These two strings of code are not explained. They are very basic things. The final effect is to enter 1 and 2, and you will get a list:
[ '40,45','116,53']
Remember, this coordinate is only the coordinate on the verification code image, not the global coordinate of the whole screen!!!!
So how to get the global coordinates? Verification code vertex (1206428) + verification code coordinates, i.e. global coordinates! Please test (1206428) here by yourself!
Step 6:
Simulate clicking and use the pyautogui module. Remember, before using this module, you must add a delay for picture switching.
(this article only studies methods and has no other meaning. Normally, it is a docking coding platform! I hope you can apply what you have learned and use these methods in other programming processes!)
for i in temp: indexs = i.split(',') x = int(indexs[0]) y = int(indexs[1]) pyautogui.click(1206+x, 428+y)
Finally, click login!
driver.find_element_by_xpath('//*[@id="J-login"]').click()
I would also like to recommend Xiaobian's Python learning area * (homonym): '832, 357, 663' whether you are Xiaobai or Daniel, Xiaobian is welcome to share dry goods from time to time, including a copy of 2021 latest Python materials and 0 basic introductory tutorial compiled by Xiaobian himself. Beginners and advanced partners are welcome. I will solve your doubts in my free time.
[click] receive