Recently, when docking requirements in the business office area, I found the “distress” of a beautiful girl. To put it simply, she had to spend more than an hour every day to input sorted data into the system.
Why distress?
Under the former want to chat to get down to business, the beautiful sister “do not think much, just talk about beautiful sister work”, although the sister, but she do such a thing has been more than a year, a commendation is perseverance, sometimes I will feel very strange, why don’t you ask the product demand, request the function of the system can support batch import?
I had a chat with a beautiful girl. I had mentioned similar needs before, but because of a series of problems such as priority and the attention of superiors, it finally came to nothing.
Pretty girls at the beginning of the complaint, as time goes by also numb, and finally become a habit…
To be honest, this is not a good situation to be in, and when your work becomes repetitive, you are likely to be replaced.
So, to save this pretty girl, I created a script using Pyhton that automatically reads data submission forms in Excel, completes the entry, and improves the accuracy of manual entry.
The sister was shocked and offered milk tea with both hands.
Some idea
In fact, similar to this type of input system, the scene of repeated work really have a lot of, sometimes really feel very troublesome, especially encounter some interaction is poor, waiting time is relatively long system.
Similar to this demand, basically divided into the following steps:
- Analysis of the page
- To simulate the login
- Assemble form data
- Cycle to submit
Of course, you also need some skills and knowledge:
- Python “Python, for example, can be implemented in other languages”
- The HTTP protocol
- Chrome debugging “Use Chrome for example, other browsers also work”
The specific implementation
Because it is the company’s system, not too convenient to stick the source code, here to record the development process in some key and pit.
The first is page analysis, this is not to say, take advantage of the powerful Chrome can.
Here focus on simulated login, often here is more pit, although the Intranet system is relatively simple, but still encounter two commonly used login verification mode.
One is that the server generates a STATE of the UUID and verifies that the incoming and generated uUID are consistent when submitting the login.
This is where you analyze the page, usually putting state in the page
This makes it easier to grab and log in using Python’s Requests and BeautifulSoup
session_requests=requests.session()
result=session_requests.get(login_url,headers=header)
tree=bs4.BeautifulSoup(result.text,'lxml')
state=tree.findAll('input',attrs={'name':'state'})[0]['value']
1
2
3
4
5
6
7Copy the code
The other is to encrypt the password when logging in, and then submit it. I didn’t notice at first, so I translated this code into Python
Def deal_password(PWD): key='asdrft745' keylength=10 code=' I =0 'for item in PWD: keyindex=i%keylength code=code+chr(ord(item)^ord(key[keyindex:keyindex+1])) i=i+1 bytesString = code.encode(encoding="utf-8") return base64.b64encode(bytesString) 1 2 3 4 5 6 7 8 9 10 11 12Copy the code
Once you’ve simulated a successful login, you’re more than halfway through, just assembling the data and submitting it using the powerful Requests call interface.
Since the forms are different, there is no code to post. The main thing is to confirm the interface data format, whether it is form-data or JSON, and do the corresponding conversion.
In addition, you should also forge the header, especially user-agent, to avoid being mistakenly killed or being checked by the company security group.
The final request frequency also need to pay attention to, do not force too hard, the system to hang “although this situation is rare”.
Finally finally, the use of their own modules, are more commonly used, there are a lot of instructions on the Internet, interested students can be actual combat, innovation work, bid farewell to repetitive labor.