pyInstaller生成的Python-EXE中获取应用程序路径

需求

实时更新这个网页的数据并下载这个网站的数据:
http://218.57.139.23:10013/iaicweb/jsp/gcloud/iaicweb/indexList/allywList.jsp

分析

通过调试,获取数据接口、字段解析处理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# ==========================数据分析============================
# 筛选词:
# 济南 济宁 枣庄 菏泽 临沂 山东 泰安 莱芜 聊城

# 办理事项 申请单位/人 办理日期 办理状态
# LCTYPE: "020501" 个体户设立登记
# LCTYPE: "020502" 个体户变更登记
# LCTYPE: "020301" 私营企业设立登记
# LCTYPE: "020302" 私营企业变更登记
# LCTYPE: "020303" 私营企业注销登记
# LCTYPE: "020304" 私营企业备案登记
# LCTYPE: "020103" 内资企业注销登记
# LCTYPE: "020102" 内资企业变更登记
# LCTYPE: "020104" 内资企业备案登记
# LCTYPE: "020401" 合作社设立登记

# function bizRender(data, type, full){
# var bizstateext = full.BIZ_STATE_EXT;
# if("10" == data){
# if("11" == bizstateext){
# return "材料指导";
# }else if("12" == bizstateext){
# return "材料指导完成";
# }else if("13" == bizstateext){
# return "受理通过";
# }
# }else if("30" == data){
# return "核准通过";
# }else if("20" == data){
# return "被驳回";
# }else if("60" == data){
# return "被退回";
# }else if("40" == data){
# return "材料指导";
# }else if("50" == data){
# return "签名";
# }
# return "";
# }

问题

使用Python环境可以运行,打包成exe就出现路径问题。
路径不统一,无法读取到配置文件。

解决方案

检查应用程序是作为脚本还是作为冻结EXE运行

1
2
3
4
5
6
7
8
# 在pyInstaller生成的Python EXE中确定应用程序路径
config_name = 'keys.txt'
# determine if application is a script file or frozen exe
if getattr(sys, 'frozen', False):
application_path = os.path.dirname(sys.executable)
elif __file__:
application_path = os.path.dirname(__file__)
config_path = os.path.join(application_path, config_name)

完整代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
__author__ = 'Risk2S'
import requests
import json
from pprint import pprint
import time
import xlwt

import os
import sys

# 文件路径
directory = './data'
if not os.path.exists(directory):
os.makedirs(directory)


# 在pyInstaller生成的Python EXE中确定应用程序路径
config_name = 'keys.txt'
# determine if application is a script file or frozen exe
if getattr(sys, 'frozen', False):
application_path = os.path.dirname(sys.executable)
elif __file__:
application_path = os.path.dirname(__file__)
config_path = os.path.join(application_path, config_name)



# http://218.57.139.23:10013/iaicweb/jsp/gcloud/iaicweb/indexList/allywList.jsp


def post_json_to_site():
url = "http://218.57.139.23:10013/iaicweb/command/ajax/com.inspur.gcloud.iaicweb.remain.cmd.QydjApplyStatusQueryCommand/queryAllywxx"

payload = "{\"params\":{\"javaClass\":\"ParameterSet\",\"map\":{\"needTotal\":true,\"limit\":-1},\"length\":4},\"context\":{\"javaClass\":\"HashMap\",\"map\":{},\"length\":0}}"
headers = {
'Content-Type': "application/json;charset=UTF-8",
'cache-control': "no-cache",
'Postman-Token': "d711a5e8-2ae3-418c-91d9-66a7a6dd99aa"
}

response = requests.request("POST", url, data=payload, headers=headers)
res_text = response.text
results = json.loads(res_text)['rows']
return results


# 返回状态
def bizRender(data,bizstateext):
if("10" == data):
if("11" == bizstateext):
return "材料指导";
elif("12" == bizstateext):
return "材料指导完成";
elif("13" == bizstateext):
return "受理通过";
elif("30" == data):
return "核准通过";
elif("20" == data):
return "被驳回";
elif("60" == data):
return "被退回";
elif("40" == data):
return "材料指导";
elif("50" == data):
return "签名";
else:
return "";


def decode_LCTYPE(data):
if("0205" == data[0:4]):
qian = "个体户";
elif("0203" == data[0:4]):
qian = "私营企业";
elif("0201" == data[0:4]):
qian = "内资企业";
elif("0204" == data[0:4]):
qian = "合作社";
else:
qian = "";
if("01" == data[4:6]):
hou = "设立登记";
elif("02" == data[4:6]):
hou = "变更登记";
elif("03" == data[4:6]):
hou = "注销登记";
elif("04" == data[4:6]):
hou = "备案登记";
else:
hou = "";
return qian+hou


word_keys = []
# 读取筛选词
with open('keys.txt','r', encoding='UTF-8') as f_key:
for key in f_key.readlines():
if key != '':
word_keys.append(key.strip())

# print(word_keys)




# for i in range(0,1):
while True:
# while False:
# 创建一个workbook 设置编码
workbook = xlwt.Workbook(encoding = 'utf-8')
# 创建一个worksheet
worksheet = workbook.add_sheet('My Worksheet',cell_overwrite_ok=True)
results = post_json_to_site()
line_key = 1
# 写入文件
for result in results:
# -----------------DUBUG------------------
# print('办理事项: ', decode_LCTYPE(result['LCTYPE']))
# print('申请单位/人: ',result['ENTNAME'])
# print('办理日期: ',result['S_EXT_TIMESTAMP']['dateStr'])
# print('办理状态: ', bizRender(result['BIZ_STATE'],result['BIZ_STATE_EXT']))
# 多条件判断
if any(word in result['ENTNAME'] for word in word_keys):
print(result['ENTNAME'])
# 写入excel
# 参数对应 行, 列, 值
worksheet.write(line_key,0, decode_LCTYPE(result['LCTYPE']))
worksheet.write(line_key,1, result['ENTNAME'])
worksheet.write(line_key,2, result['S_EXT_TIMESTAMP']['dateStr'])
worksheet.write(line_key,3, bizRender(result['BIZ_STATE'],result['BIZ_STATE_EXT']))
line_key += 1
# 保存
workbook.save('./data/Excel_test{localtime}.xls'.format(localtime=time.strftime("%Y-%m-%d=%H-%M-%S", time.localtime())))
time.sleep(60)