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
|
import json
import re
from lxml import etree
def write(path, data): with open(path, 'w', encoding='utf-8') as f: _data = json.dumps(data, ensure_ascii=False) f.write(_data) return True
list_end = []
for i in range(1, 48): path = 'data/%s.html' % i f = open(path, 'r', encoding='utf-8') st = f.read() html = etree.HTML(st) all_list = [] _id = html.xpath('/html/body/div[2]/div[2]/div/table/tbody/tr') title = html.xpath('//*//font') a = 0 c = '、' for index in range(len(_id)): new_title = title[a] if '、' in new_title.text and bool(re.search(r'\d', new_title.text)): c = c.split(sep='、', maxsplit=-1) if c[0].isdigit(): c.remove(c[0]) c = '、'.join(c) all_list.append(c) c = new_title.text else: if bool(re.search(r'\d', new_title.text)) or '、' in new_title.text: print('请确认这不是标题 || ', new_title.text) c += '|' + new_title.text c = c.replace('\n', '').replace('\r', '').replace('\t', '') a += 1 all_list.remove(all_list[0]) all_list.append('##$%#%$#%$#$@$$%^#%$@#%$!#^$%$#^%')
a = 0 list_temp = [] for index in range(len(_id)): id_num = dict(_id[a].attrib) list_temp.append(id_num) a += 1
list_emd = [] for x in list_temp: if bool(x): list_emd.append(x)
for index in range(len(list_emd)): list_emd[index]['question_txt'] = all_list[index] list_emd[index]['answer'] = '正确答案填在此' list_emd[index]['answer_check_1'] = '检验成员1的答案' list_emd[index]['answer_check_2'] = '检验成员2的答案' list_emd[index]['answer_check_3'] = '检验成员3的答案' list_emd[index]['answer_check_4'] = '检验成员4的答案' list_emd[index]['answer_check_5'] = '检验成员5的答案'
list_emd[index]['answer_txt'] = '使用说明:将正确答案填入answer的引号中就可,多选不用间隔,示例 *A* *ABCD*' list_emd[index]['id'] = '3' del list_emd[index]['id'] list_end.append(list_emd)
list_end = sum(list_end, [])
def deletedup(li): seen = set() new_list_2 = [] print(li) for d in li: d2 = d['question_txt'] str1 = d['question_txt'] if str1 == '##$%#%$#%$#$@$$%^#%$@#%$!#^$%$#^%': continue if d2 not in seen: new_list_2.append(d) seen.add(d2) return new_list_2
if __name__ == '__main__': list_tools = deletedup(list_end) print(list_tools) print('总计:', len(list_tools), '条数据', '\n注意:请划到顶部确认那些东西是不是标题!!!!') out_result = write('result.json', list_tools) print('是否成功写入result.json文件:', out_result)
|