-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathresult_win_bid.py
420 lines (394 loc) · 17.8 KB
/
result_win_bid.py
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
# -*- coding: utf-8 -*-
import _thread
import ast
import json
import sqlite3
import time
import traceback
import urllib
import numpy as np
import openpyxl
import requests
from bs4 import BeautifulSoup
import pandas as pd
from tqdm import tqdm
import spider_category
# 配置和使用代理池,避免被封ip
PROXIES = {}
# 编码:中标成交结果公告
CATEGORY_CODE_RESULT_WIN_BID = 'ZcyAnnouncement3004'
CATEGORY_NAME = '中标成交结果公告'
# 公告详情对象
class CategoryDetail():
# 分类
category = ''
# 文章标题
title = ""
# 项目编号
project_code = ""
# 项目名称
project_name = ""
# 文章发布日期
publish_date = ""
# 文章链接
url = ""
# 中标价格
win_bid_price = ""
# 中标供应商名称
win_bid_supply_name = ""
# 中标供应商地址
win_bid_supply_addr = ""
# 文章内容
content = ""
def __init__(self, category='', title='', project_code='', project_name='', publish_date='',
url='', win_bid_price='', win_bid_supply_name='', win_bid_supply_addr='', content=''):
self.category = category
self.title = title
self.project_code = project_code
self.project_name = project_name
self.publish_date = publish_date
self.url = url
self.win_bid_price = win_bid_price
self.win_bid_supply_name = win_bid_supply_name
self.win_bid_supply_addr = win_bid_supply_addr
self.content = content
# 爬取中标成交结果公告
def spider(keyword='', start_time='', end_time=''):
# step1:抓取网页数据
category_details = __spider_web_page(keyword=keyword, start_time=start_time, end_time=end_time)
# step2:解析网页数据
category_details = __analyze_web_page(category_details)
return category_details, keyword, start_time, end_time
# 爬取中标成交结果公告。客户端用,结果导出到excel
def spider_with_client(keyword='', start_time='', end_time=''):
category_details, keyword, start_time, end_time = spider(keyword=keyword, start_time=start_time, end_time=end_time)
# step3:数据导出到excel文件
file_name = __set_excel_file_name(keyword, start_time, end_time)
file_path = spider_category.EXCEL_OUT_PUT_DIR + file_name
__export_excel(category_details, file_path)
# 爬取中标成交结果公告。服务端用,结果存入数据库
def spider_with_server(keyword='', start_time='', end_time=''):
category_details, keyword, start_time, end_time = spider(keyword=keyword, start_time=start_time, end_time=end_time)
# step3:数据写入数据库
__save_db(keyword, category_details)
# 导出数据到excel
def export_excel_from_db(keyword='', start_time='', end_time=''):
category_details = __select_db(keyword, start_time, end_time)
file_name = __set_excel_file_name(keyword, start_time, end_time)
file_path = spider_category.EXCEL_OUT_PUT_DIR + file_name
__export_excel(category_details, file_path)
return file_path
# 数据查询
def query_data(keyword='', start_time='', end_time=''):
data = []
category_details = __select_db(keyword, start_time, end_time)
for detail in category_details:
# 对象转json,并去掉转义符
json_str = json.dumps(detail.__dict__, ensure_ascii=False)
# ast.literal_eval语法解析不支持带有值为null
json_str = json_str.replace(': null', ': ""')
json_str = ast.literal_eval(json_str)
data.append(json_str)
return data
# 初始化数据
def init_db_data():
conn = None
try:
conn = sqlite3.connect(spider_category.DB_PATH)
# 配置中文分词器
# TODO 重新编译sqlite3加入tokenize=icu中文分词模块
# conn.execute('''CREATE VIRTUAL TABLE IF NOT EXISTS category_result_win_bid
# USING fts4(project_code TEXT PRIMARY KEY NOT NULL,
# category TEXT, project_name TEXT, title TEXT, publish_date date,
# win_bid_price TEXT, win_bid_supply_name TEXT, win_bid_supply_addr TEXT,
# url TEXT, content TEXT, tokenize=icu);''')
conn.execute('''CREATE TABLE IF NOT EXISTS category_result_win_bid
(project_code TEXT PRIMARY KEY NOT NULL,
category TEXT, project_name TEXT, title TEXT, publish_date date,
win_bid_price TEXT, win_bid_supply_name TEXT, win_bid_supply_addr TEXT,
url TEXT, content TEXT);''')
rows = conn.execute("SELECT count(*) from category_result_win_bid").fetchall()
row = rows[0]
if row[0] == 0:
# 异步执行,避免阻塞主线程
_thread.start_new_thread(spider_with_server, ('', time.strftime("%Y-01-01", time.localtime()), ''))
except:
traceback.print_exc()
return False
finally:
if conn != None:
try:
conn.close()
except:
traceback.print_exc()
def __select_db(keyword='', start_time='', end_time=''):
category_details = []
conn = None
try:
conn = sqlite3.connect(spider_category.DB_PATH)
query_sql = ' 1 == 1'
# TODO 配置中文分词器后,改用match 'A B'查询
if keyword != '' and keyword != None:
match_sql = ""
dicts = spider_category.get_category_name_by_title(keyword)
for dict in dicts:
match_sql += " and title like '%" + dict + "%'"
query_sql += match_sql
if start_time != '' and start_time != None:
query_sql += ' and date(publish_date) >= ' + "'" + start_time + "'"
if end_time != '' and end_time != None:
query_sql += ' and date(publish_date) <= ' "'" + end_time + "'"
rows = conn.execute("SELECT * from category_result_win_bid where" + query_sql + " order by publish_date desc").fetchall()
for row in rows:
category_detail = CategoryDetail()
category_details.append(category_detail)
category_detail.project_code = row[0]
category_detail.category = row[1]
category_detail.project_name = row[2]
category_detail.title = row[3]
category_detail.publish_date = row[4]
category_detail.win_bid_price = row[5]
category_detail.win_bid_supply_name = row[6]
category_detail.win_bid_supply_addr = row[7]
category_detail.url = row[8]
# category_detail.content = row[9]
return category_details
except:
traceback.print_exc()
return category_details
finally:
if conn != None:
try:
conn.close()
except:
traceback.print_exc()
# 抓取网页数据
def __spider_web_page(keyword='', start_time='', end_time=''):
category_details = []
try:
article_id_url_d = __page_query_category(keyword=keyword, start_time=start_time, end_time=end_time)
# 进度条打印到控制台
for article_id, url in tqdm(article_id_url_d.items(), desc='【第一步】爬取网页', colour='GREEN', disable=spider_category.PROGRESS_BAR_DISABLE):
# 暂停,模拟人的浏览操作行为,避免爬取过快被服务端识别
time.sleep(1)
data = __query_category_detail(url)
category_detail = CategoryDetail()
category_details.append(category_detail)
category_detail.title = data.get("title")
category_detail.project_code = data.get("projectCode")
category_detail.project_name = data.get("projectName")
category_detail.content = data.get("content")
if data.get("publishDate") != None and len(str(data.get("publishDate"))) == 13:
# 13位毫秒时间戳转年月日
category_detail.publish_date = time.strftime("%Y-%m-%d", time.localtime(float(data.get("publishDate") / 1000)))
# 根据文章id获取对应页面链接
if data.get("announcementLinkDtoList") != None and len(data.get("announcementLinkDtoList")) > 0:
for link_dto in data.get("announcementLinkDtoList"):
if article_id == link_dto.get("articleId"):
category_detail.url = link_dto.get("url")
category_detail.publish_date = link_dto.get("publishDate")
break
except:
traceback.print_exc()
return category_details
# 解析和清洗网页数据
def __analyze_web_page(category_details=[]):
# 进度条打印到控制台
for category_detail in tqdm(category_details, desc='【第二步】解析网页', colour='GREEN', disable=spider_category.PROGRESS_BAR_DISABLE):
try:
# 使用bs4库的html解析器
# print(BeautifulSoup(category_detail.content, "html.parser").prettify())
soup = BeautifulSoup(category_detail.content, "html.parser")
# 策略一:抽取文章表格内带有特定样式的内容
rs, category_detail = __analyze_article_table(soup, category_detail)
if rs:
continue
# 策略二:抽取文章第一个表格内容,按关键字搜索
rs, category_detail = __analyze_article_table_content(soup, category_detail)
if rs:
continue
# 策略三:抽取文章所有行,按关键字搜索
rs, category_detail = __analyze_article_p(soup, category_detail)
if rs:
continue
except:
traceback.print_exc()
return category_details
# 导出excel
def __export_excel(category_details=[], file_path=''):
data = []
# 进度条打印到控制台
for i in tqdm(range(len(category_details)), desc='【第三步】导出excel', colour='GREEN', disable=spider_category.PROGRESS_BAR_DISABLE):
category_detail = category_details[i]
dic = {}
dic['序号'] = str(i + 1)
dic['标题'] = category_detail.title
dic['发布时间'] = category_detail.publish_date
dic['项目编号'] = category_detail.project_code
dic['项目名称'] = category_detail.project_name
dic['中标成交价格'] = category_detail.win_bid_price
dic['供应商名称'] = category_detail.win_bid_supply_name
dic['供应商地址'] = category_detail.win_bid_supply_addr
dic['公告地址'] = category_detail.url
data.append(dic)
__export_excel_auto_column_weight(file_path, data)
# 存数据库
def __save_db(keyword, category_details=[]):
conn = None
try:
conn = sqlite3.connect(spider_category.DB_PATH)
for category_detail in category_details:
if category_detail.project_code == '':
continue
data = []
data.append(category_detail.project_code)
# TODO data.append(keyword)
data.append('')
data.append(category_detail.project_name)
data.append(category_detail.title)
data.append(category_detail.publish_date)
data.append(category_detail.win_bid_price)
data.append(category_detail.win_bid_supply_name)
data.append(category_detail.win_bid_supply_addr)
data.append(category_detail.url)
data.append(category_detail.content)
conn.execute('insert or ignore into category_result_win_bid VALUES (?,?,?,?,?,?,?,?,?,?)', data)
conn.commit()
return True
except:
traceback.print_exc()
return False
finally:
if conn != None:
try:
conn.close()
except:
traceback.print_exc()
# 分页查询项目
def __page_query_category(keyword='', start_time='', end_time=''):
# key=文章id,value=文章详情url
article_id_url_d = {}
total = __query_category(keyword=keyword, page_no=1, start_time=start_time, end_time=end_time).get('total')
print('查询结果:' + str(total) + '条')
page = total // 100
if total % 100 > 0:
page = page + 1
for i in range(1, page + 1):
# 暂停,模拟人的浏览操作,避免爬取过快被服务端识别
time.sleep(1)
datas = __query_category(keyword=keyword, page_no=i, start_time=start_time, end_time=end_time).get('data')
for data in datas:
article_id = data.get('articleId')
# 对参数encode
url = "http://zfcg.gxzf.gov.cn/portal/detail?articleId=" + urllib.parse.quote(article_id)
article_id_url_d[article_id] = url
return article_id_url_d
# 查询项目
def __query_category(keyword='', page_no=1, start_time='', end_time=''):
category_url = "http://zfcg.gxzf.gov.cn/portal/category"
reqBody = {"pageNo": page_no, "pageSize": 100, "publishDateBegin": start_time, "publishDateEnd": end_time,
"categoryCode": CATEGORY_CODE_RESULT_WIN_BID, "keyword": keyword}
if len(PROXIES) > 0:
response = requests.post(url=category_url, json=reqBody, headers=spider_category.HEADERS, proxies=PROXIES, verify=False)
else:
response = requests.post(url=category_url, json=reqBody, headers=spider_category.HEADERS, verify=False)
if response.content:
return response.json().get('result').get('data')
# 查询项目详情
def __query_category_detail(category_url=''):
if len(PROXIES) > 0:
response = requests.get(url=category_url, headers=spider_category.HEADERS, proxies=PROXIES, verify=False)
else:
response = requests.get(url=category_url, headers=spider_category.HEADERS, verify=False)
if response.content:
return response.json().get("result").get("data")
# 抽取文章表格内带有特定样式的内容
def __analyze_article_table(soup, category_detail):
rs = soup.find_all('td', {'class': 'code-summaryPrice'})
if len(rs) == 0:
return False, category_detail
category_detail.win_bid_price = rs[0].get_text(strip=True)
category_detail.win_bid_supply_name = soup.find_all('td', {'class': 'code-winningSupplierName'})[0].get_text(strip=True)
category_detail.win_bid_supply_addr = soup.find_all('td', {'class':'code-winningSupplierAddr'})[0].get_text(strip=True)
return True, category_detail
# 抽取文章所有行,按关键字搜索
def __analyze_article_p(soup, category_detail):
rs = False
ps = soup.find_all('p')
for p in ps:
# 判断是否包含关键字,按中文冒号分隔
text = p.get_text(strip=True)
if text == "":
continue
if "中标" in text and "金额" in text and category_detail.win_bid_price == '':
category_detail.win_bid_price = text
rs = True
continue
if "供应商名称" in text and category_detail.win_bid_supply_name == '':
category_detail.win_bid_supply_name = text
rs = True
continue
if "供应商地址" in text and category_detail.win_bid_supply_addr == '':
category_detail.win_bid_supply_addr = text
rs = True
continue
return rs, category_detail
# 抽取文章第一个表格内容,按关键字搜索
def __analyze_article_table_content(soup, category_detail):
tbs = soup.find_all('table')
if len(tbs) == 0:
return False, category_detail
# 取第一个表格第三列内容
# 将换行符<br />替换为特殊标记,获取内容后再按标记分隔。也可以先查找所有br标签后,使用next_siblings()等函数移动查找上下节点。也可以使用正则表达式捕获特定内容
if len(tbs[0].find_all('td')[5].find_all('br')) < 2:
return False, category_detail
for br in tbs[0].find_all('td')[5].find_all('br'):
br.replace_with(spider_category.HTML_BR_REPLACE_TARGET)
lines = tbs[0].find_all('td')[5].find_all('p')[0].get_text(strip=True).split(spider_category.HTML_BR_REPLACE_TARGET)
category_detail.win_bid_supply_name = __split_colon(lines[0])
category_detail.win_bid_supply_addr = __split_colon(lines[1])
category_detail.win_bid_price = __split_colon(lines[2])
return True, category_detail
# 导出excel并自动设置列宽
def __export_excel_auto_column_weight(file_path, data, sheet_name='Sheet1'):
df = pd.DataFrame(data)
writer = pd.ExcelWriter(file_path, engine='openpyxl')
# 需要设置index=False,否则后续列宽计算有误
df.to_excel(writer, index=False)
# 数据输出到excel后,自动设置列宽
try:
# 计算表头的字符宽度
column_widths = (
df.columns.to_series().apply(lambda x: len(x.encode('gbk'))).values
)
# 计算每列的最大字符宽度
max_widths = (
df.astype(str).applymap(lambda x: len(x.encode('gbk'))).agg(max).values
)
# 计算整体最大宽度
widths = np.max([column_widths, max_widths], axis=0)
# 设置列宽
worksheet = writer.sheets[sheet_name]
for i, width in enumerate(widths, 1):
# openpyxl引擎设置字符宽度时会缩水0.5左右个字符,所以干脆+2使左右都空出一个字宽。
worksheet.column_dimensions[openpyxl.utils.get_column_letter(i)].width = width + 2
except:
traceback.print_exc()
# 保存设置
writer.save()
print('结果导出完成:' + file_path)
# 冒号分隔,处理比如'投标价格:123元'=>'123元'
def __split_colon(str=''):
if ':' in str:
return str.split(':')[1]
if ':' in str:
return str.split(':')[1]
return str
# 生成输出文件名,包含搜索关键字、起始结束时间
def __set_excel_file_name(keyword, start_time, end_time):
file_name = CATEGORY_NAME
if keyword != '':
file_name += '-' + keyword
file_name += "-时间" + '(' + start_time + '~' + end_time + ')'
file_name += '.xlsx'
return file_name