# morgoth **Repository Path**: rothschil/morgoth ## Basic Information - **Project Name**: morgoth - **Description**: No description available - **Primary Language**: Unknown - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2021-09-28 - **Last Updated**: 2022-09-03 ## Categories & Tags **Categories**: Uncategorized **Tags**: Spider, Python, 行政区划, 国家统计局 ## README ## 1. 说明 这是本人使用 `Python` 编写第一个爬虫软件,命名方式依然采用个人钟爱一部文学巨著 `指环王` 来命名,当然这次是用原著中的一个超级大反派,并没有其他含义,纯粹是随心胡乱编写而已。 本工程截止当前主要完成两类数据的爬取。 - 全国统计用区划代码和城乡划分代码 ![20210929180146](https://abram.oss-cn-shanghai.aliyuncs.com/blog/gradle/20210929180146.png) - 中国电信阳光采购网公告信息 ![20210929180406](https://abram.oss-cn-shanghai.aliyuncs.com/blog/github/python/20210929180406.png) ## 2. 工程 ~~~cmd │ .gitignore │ amaz.py │ amaz.ui ------Qt UI界面 │ announ.py ------公告业务逻辑 │ main.py ------区域业务逻辑 │ README.md ├─po │ │ Announcement.py ------公告实体 │ │ Area.py ------区域实体类 │ └─util │ DbUtil.py ------数据库工具类 ~~~ ### 2.1. 区划代码 区域在现实中是有层级概念的,包含:省/直辖市/自治区、地级市、区县、乡/镇/街道、居委会/村,界面中元素信息对每个区域的地址有定义,所以我们需要按照每一个层级定义的地址链接进行封装再拼接,最终访问获取响应内容,再进行解析。因为涉及到按照一定规则读取数据,所以使用数据库进行持久化。 执行过程中按照层级自上而下,依次执行,当然也可以将所有执行逻辑写在一起,自动执行,考虑到数据量比较大,访问频次比较高,一次性全部执行完毕的方式不推荐。 数据库表格信息如下: ~~~sql CREATE TABLE tb_locations ( local_code VARCHAR(30) DEFAULT NULL, local_name VARCHAR(100) DEFAULT NULL, lv INT(11) DEFAULT NULL, sup_local_code VARCHAR(30) DEFAULT NULL, flag VARCHAR(6) DEFAULT NULL, url VARCHAR(60) DEFAULT NULL ) ; ~~~ ### 2.2. 公告 ## 3. GUI环境构建 由于我安装的是压缩版的 `Python` 环境,没有自带 `Gui`,采用 `Python3`、`PyQt5`、 `PyCharm` 桌面 `GUI` 开发环境搭建。 ### 3.1. 安装PyQt5 ~~~cmd >> pip3 install PyQt5 >> pip3 install PyQt5-tools ~~~ ![20211002174009](https://abram.oss-cn-shanghai.aliyuncs.com/blog/github/python/20211002174009.png) 验证下是否安装成功 `pip3 list` ![20211002174324](https://abram.oss-cn-shanghai.aliyuncs.com/blog/github/python/20211002174324.png) ### 3.2. 配置Pycharm 在 Pycharm 编译器,设置中, #### 3.2.1. 点击 `File` -> `Settings` ![20211002174655](https://abram.oss-cn-shanghai.aliyuncs.com/blog/github/python/20211002174655.png) #### 3.2.2. `Tools` -> `External Tools` -> 点击 + 号 ![20211002174500](https://abram.oss-cn-shanghai.aliyuncs.com/blog/github/python/20211002174500.png) #### 3.2.3. 设置 `Qt Designer` `Programs` 参数需要修改为你环境中实际 `designer.exe` 的路径,其他默认即可。 ~~~cmd Name:Qt Designer Programs:D:\ProgramFiles\Python\3.9.6\Lib\site-packages\qt5_applications\Qt\bin\designer.exe Working directory:$ProjectFileDir$ ~~~ ![20211002175159](https://abram.oss-cn-shanghai.aliyuncs.com/blog/github/python/20211002175159.png) #### 3.2.4. 配置PyUIC `Programs` 参数需要修改为你环境中实际 `python.exe` 的路径,其他默认即可。 ~~~cmd Name:PyUIC Programs:D:\ProgramFiles\Python\3.9.6\python.exe Working directory:$ProjectFileDir$ ~~~ ![20211002175630](https://abram.oss-cn-shanghai.aliyuncs.com/blog/github/python/20211002175630.png) ### 3.3. 使用 `Qt Designer` 完成上述配置后,我们开发环境中会有我们配置的内容,利用这两个拓展工具可以实现 GUI 环境的开发。二者作用如下: - `Qt Designer` 完成 `UI` 的基础设计 - `PyUIC` 会将设计好的 `UI` 文件转换为 `python` 文件 ![20211002175933](https://abram.oss-cn-shanghai.aliyuncs.com/blog/github/python/20211002175933.png) - 启动 `Qt Designer` ,窗体组件,中有 `templates\forms` 选中 `Widget` ,其他选项默认即可,点击 `创建` ![20211002180259](https://abram.oss-cn-shanghai.aliyuncs.com/blog/github/python/20211002180259.png) - 这时候的工作就是拖拽左侧的工具选区,至我们的工作区 ![20211002180644](https://abram.oss-cn-shanghai.aliyuncs.com/blog/github/python/20211002180644.png) ![20211002180852](https://abram.oss-cn-shanghai.aliyuncs.com/blog/github/python/20211002180852.png) - 完成界面操作后,保存至目录下,并命名,将会看到 后缀为 `.ui` 的文件 ![20211002181020](https://abram.oss-cn-shanghai.aliyuncs.com/blog/github/python/20211002181020.png) ![20211002181120](https://abram.oss-cn-shanghai.aliyuncs.com/blog/github/python/20211002181120.png) ### 3.4. 使用 `PyUIC` 将 `UI` 文件转换程 python 执行文件,使用 `PyUIC` 工具。此时会生成一个 与 `UI` 文件同名的 `python` 文件。 ![20211002181334](https://abram.oss-cn-shanghai.aliyuncs.com/blog/github/python/20211002181334.png) ![20211002181559](https://abram.oss-cn-shanghai.aliyuncs.com/blog/github/python/20211002181559.png) 为了能够执行执行该 `python` 文件,我们对 `python` 文件,末尾处添加 `__main__` 方法 ~~~py # -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'amaz.ui' # # Created by: PyQt5 UI code generator 5.15.4 # # WARNING: Any manual changes made to this file will be lost when pyuic5 is # run again. Do not edit this file unless you know what you are doing. from PyQt5 import QtCore, QtGui, QtWidgets class Ui_Form(object): def setupUi(self, Form): Form.setObjectName("Form") Form.resize(1121, 796) self.lineEdit = QtWidgets.QLineEdit(Form) self.lineEdit.setGeometry(QtCore.QRect(390, 250, 351, 20)) self.lineEdit.setObjectName("lineEdit") self.label = QtWidgets.QLabel(Form) self.label.setGeometry(QtCore.QRect(300, 250, 54, 12)) self.label.setObjectName("label") self.label_2 = QtWidgets.QLabel(Form) self.label_2.setGeometry(QtCore.QRect(300, 310, 54, 12)) self.label_2.setObjectName("label_2") self.lineEdit_2 = QtWidgets.QLineEdit(Form) self.lineEdit_2.setGeometry(QtCore.QRect(390, 310, 351, 20)) self.lineEdit_2.setObjectName("lineEdit_2") self.pushButton = QtWidgets.QPushButton(Form) self.pushButton.setGeometry(QtCore.QRect(390, 400, 75, 23)) self.pushButton.setObjectName("pushButton") self.pushButton_2 = QtWidgets.QPushButton(Form) self.pushButton_2.setGeometry(QtCore.QRect(580, 400, 75, 23)) self.pushButton_2.setObjectName("pushButton_2") self.retranslateUi(Form) QtCore.QMetaObject.connectSlotsByName(Form) def retranslateUi(self, Form): _translate = QtCore.QCoreApplication.translate Form.setWindowTitle(_translate("Form", "Form")) self.label.setText(_translate("Form", "姓名")) self.label_2.setText(_translate("Form", "证件号")) self.pushButton.setText(_translate("Form", "重置")) self.pushButton_2.setText(_translate("Form", "提交")) if __name__=="__main__": import sys from PyQt5.QtGui import QIcon app=QtWidgets.QApplication(sys.argv) widget=QtWidgets.QWidget() ui=Ui_Form() ui.setupUi(widget) #增加icon图标 widget.setWindowIcon(QIcon('web.png')) widget.show() sys.exit(app.exec_()) ~~~ 直接运行,图形化界面如下: ![20211002182019](https://abram.oss-cn-shanghai.aliyuncs.com/blog/github/python/20211002182019.png)