Software: Pycharm
Environment: Python 3.7. 9 (Considering that customers may have different operating systems, for compatibility reasons)
Technical library: requests, pandas, Pyqt5, etc. (see dependency files for details)
Through analysis and communication with customer demand documents, there are roughly the following requirements:
Submit data to 3 interfaces in batches based on "single number ownership"
Need a GUI operation interface
Support different salesmen to log in
In general It is said to be a POST data submission and GUI development.
This section mainly uses crawler technology. The steps that remain unchanged for thousands of years are to analyze the web page first.
Through packet capture, it is found that the password is plain text, which reduces the difficulty by half. Then use the correct password to analyze the return after successful login. .
def login(self, username: str, password: str): """ 登录 """ url = "http://cloud.tiamaes.com:11349/erp/portal.bootstrap/SSOLoginAction/login.do" data = { "_tp_data": '{"parameters":{"userName":' + username + ',"pwd":' + password + '},"rowsets":{},"headers":{},"requestComponent":"0"}' } data = parse.urlencode(data).replace("+", "") resp = requests.post(url, headers=self.headers, data=data, verify=False) self.IDENTIFIER = resp.json()["headers"]["IDENTIFIER"] return self.IDENTIFIER
It is found that after successful login, an "IDENTIFIER" parameter will be returned. The value is an encrypted string. This is obvious. Just by looking at the literal meaning, you know that this must be useful, so Write it down first.
Since I am using a test account, the data submitted by this account will be deleted. In order not to inject too much invalid data into others, there will be no actual entry here. , explained with business code.
Get vehicle information
Through analysis, it was found that although the customer has given some vehicle information, there is still a lot of missing information that requires oneself Replenish. Through packet capture, we found that after entering the vehicle number, an Ajax request will be initiated, and the other information in the form is the data returned by the Ajax request.
def get_car_details(self, car_no: str, IDENTIFIER: str): """ 获取车辆信息 """ # print(self.IDENTIFIER) url = "http://cloud.tiamaes.com:11349/money/basis.inter/JwBusAction/getCacheJwBusByNo.do" data = { '_tp_data': '{"parameters": {"busNo": ' + str(car_no) + ', "dsName": "83"}, "rowsets": {}, "headers": {"IDENTIFIER": ' + IDENTIFIER + '}, "requestComponent": "0"}' } data = parse.urlencode(data).replace("+", "") resp = requests.post(url, headers=self.headers, data=data, verify=False) rows = resp.json()["rowsets"]["com.tp.basis.entity.entity.bus.BaJwBus"]["rows"][0] return rows
Get personnel information
I didn’t find the personnel information in the form through packet capture, and later found it Relevant data was found on a page.
This is a little more troublesome. You need to use regular expressions to match the data.
def get_personal_info(self, IDENTIFIER: str): """ 获取个人信息 """ url = "http://cloud.tiamaes.com:11349/money/money.action/CharteredAction/showDetail.do" data = { '_tp_data': '{"parameters":{"dsName":"83","method":"add","recId":"-1"},"rowsets":{},"headers":{"IDENTIFIER":' + IDENTIFIER + '},"requestComponent":"1"}' } data = parse.urlencode(data).replace("+", "") resp = requests.post(url, headers=self.headers, data=data, verify=False) json_data = eval(re.findall(r'<code>.*?"rows":\[(.*?)\]', resp.text)[0]) return json_data
Initiate a request and submit the data
Get the identifier, vehicle information, and personnel information returned by the login, and the rest is to communicate with the customer Combine the given data and initiate a request. It should be noted that the request parameters need to be converted to URL encoding. The request parameters are also the most troublesome part of this crawler. Here we show you the parameters that need to be sent in a request.
#There are many parameters and the format requirements are relatively strict. In the entire development process, debugging here also takes the longest time. After debugging, the code should be simplified. I am too lazy to change the merge that needs to be merged after debugging, so this section is relatively redundant.
def submit_data(self, i: dict, IDENTIFIER: str): """ 众意数据提交 """ personal_info = self.get_personal_info(IDENTIFIER) # 获取个人信息 personal_info_data = str(personal_info).replace("'", '"') # 将personal_info转换为字符串 url = "http://cloud.tiamaes.com:11349/money/money.action/CharteredAction/saveForm.do" print(f'开始处理--{i["单号归属"]}--数据') memo = f'工单号{i["工单号"]}、餐费{i["餐费"]}、住宿{i["住宿"]}、过路过桥费{i["过路过桥费"]}、油费{i["油费"]}、备注{i["备注"]}' # 拼接备注信息 car_infos = self.get_car_details(str(i["车号"]), IDENTIFIER) # 获取车辆信息 pay_type = { "现金": "3", "转账": "2", "欠款": "1" } single_and_double = { "单程": "1", "双程": "2" } colType = pay_type[i["结账方式"]] # 获取结账方式编码 oddEven = single_and_double[i["单双程"]] # 获取单双程编码 now_date = datetime.datetime.now().date().strftime("%Y-%m-%d") # 获取当前日期 .......(此处省略) data["_tp_data"] = data["_tp_data"].replace('"dsName":"83"', '"dsName":"82"') data = parse.urlencode(data).replace("+", "") # 将字典转换成url编码 resp = requests.post(url, headers=self.headers, data=data, verify=False).json() order_id = resp["rowsets"]["com.tp.money.entity.basic.Chartered"]["rows"][0]["recNo"] # 获取订单编号 i["包车单号"] = order_id return data
gui development is relatively simple. If you don’t want to beautify it, Pyqt’s native plug-in will do. I borrowed the experience of the previous project here and used only Some knowledge has made a borderless interface and appropriate beautification.
Login
from PyQt5.QtCore import Qt from PyQt5.QtGui import QColor from PyQt5.QtWidgets import (QFrame, QMessageBox, QGraphicsDropShadowEffect) from Ui import login_ui from Ui.submit_ui_main import MySubmitForm from submit import TransitSubmit class MyLogin(login_ui.Ui_LoginForm, QFrame): def __init__(self, submit: TransitSubmit): super().__init__() # self.IDENTIFIER = None # self.my_main_window = None self.setupUi(self) self.submit = submit # 设置无边框模式 self.setWindowFlag(Qt.FramelessWindowHint) # 将界面设置为无框 self.setAttribute(Qt.WA_TranslucentBackground) # 将界面属性设置为半透明 self.shadow = QGraphicsDropShadowEffect() # 设定一个阴影,半径为10,颜色为#444444,定位为0,0 self.shadow.setBlurRadius(10) self.shadow.setColor(QColor("#444444")) self.shadow.setOffset(0, 0) self.frame.setGraphicsEffect(self.shadow) # 为frame设定阴影效果 # ------------------------------------------------ self.show() self.pushButton_3.clicked.connect(self.close) # 关闭按钮 self.pushButton_login.clicked.connect(self.do_login) # 登录按钮 # 以下是控制窗口移动的代码 def mousePressEvent(self, event): # 鼠标左键按下时获取鼠标坐标,按下右键取消 if event.button() == Qt.LeftButton: self.m_flag = True self.m_Position = event.globalPos() - self.pos() event.accept() elif event.button() == Qt.RightButton: self.m_flag = False def mouseMoveEvent(self, QMouseEvent): # 鼠标在按下左键的情况下移动时,根据坐标移动界面 if Qt.LeftButton and self.m_flag: self.move(QMouseEvent.globalPos() - self.m_Position) QMouseEvent.accept() def mouseReleaseEvent(self, QMouseEvent): # 鼠标按键释放时,取消移动 self.m_flag = False # 登录事件 def do_login(self): username = self.lineEdit_username.text() password = self.lineEdit_password.text() if not username or not password: QMessageBox.warning(self, '警告', '用户名或密码不能为空', QMessageBox.Yes) return else: IDENTIFIER = self.submit.login(username, password) if not IDENTIFIER: QMessageBox.warning(self, '警告', '用户名或密码错误', QMessageBox.Yes) return self.hide() # 隐藏登录界面 my_submit_form = MySubmitForm(self.submit, IDENTIFIER) my_submit_form.exec_() # 显示主界面
Business Operation
class MySubmitForm(submitform_ui.Ui_Dialog_Submit, QDialog): def __init__(self, submit: TransitSubmit, IDENTIFIER: str): super().__init__() ...... self.setupUi(self) ...... self.progressBar.hide() # 关闭进度条显示 self.setWindowFlags(Qt.FramelessWindowHint) # 无边框 self.setAttribute(Qt.WA_TranslucentBackground) # 设置窗口透明 self.pushButton_mini.clicked.connect(self.showMinimized) # 实现最小化 self.pushButton_close.clicked.connect(self.close) # 实现关闭功能 ...... self.show() # 实现鼠标拖拽功能 def mousePressEvent(self, event): self.pressX = event.x() # 记录鼠标按下的时候的坐标 self.pressY = event.y() def mouseMoveEvent(self, event): x = event.x() y = event.y() # 获取移动后的坐标 moveX = x - self.pressX moveY = y - self.pressY # 计算移动了多少 positionX = self.frameGeometry().x() + moveX positionY = self.frameGeometry().y() + moveY # 计算移动后主窗口在桌面的位置 self.move(positionX, positionY) # 移动主窗口 ......
Let me say more here. At first, I used QFrame the same as login, but it does not have an exec() method. It cannot pop up after successful login. It may also be that my knowledge is limited and I cannot do it. By analyzing the source code, I found that QDialog has this method to achieve pop-up. Later, I changed it and used QDialog to create a borderless interface.
I won’t say much about the rest of the packaging. There are many tutorials on the Internet. What I use here is D packaging, upx compression, and changed icons. The entire packaged project is more than 50 MB.
The above is the detailed content of How to automatically enter ERP system data in Python. For more information, please follow other related articles on the PHP Chinese website!