@@ -141,8 +141,12 @@ def match_field(field_key: str, pattern: Union[Pattern, List[Pattern]], text):
141141 field_value = match_field (field_name , pattern_value , html )
142142
143143 if field_value is None :
144- write_text ('./resp.txt' , html ) # debug
145- raise AssertionError (f"文本没有匹配上字段:字段名为'{ field_name } ',pattern: [{ pattern_value .pattern } ]" )
144+ JmModuleConfig .raise_regex_error_executor (
145+ f"文本没有匹配上字段:字段名为'{ field_name } ',pattern: [{ pattern_value .pattern } ]" ,
146+ html ,
147+ field_name ,
148+ pattern_value
149+ )
146150
147151 # 保存字段
148152 field_dict [field_name ] = field_value
@@ -167,9 +171,7 @@ def format_url(cls, path, domain=None):
167171
168172class JmSearchSupport :
169173 # 用来缩减html的长度
170- pattern_html_search_shorten_for = compile ('<div class="well well-sm">([\s\S]*)'
171- '<div class="row">[\s\S]*'
172- '<div class="bot-per visible-xs visible-sm">' )
174+ pattern_html_search_shorten_for = compile ('<div class="well well-sm">([\s\S]*)<div class="row">' )
173175
174176 # 用来提取搜索页面的的album的信息
175177 pattern_html_search_album_info_list = compile (
@@ -185,9 +187,24 @@ class JmSearchSupport:
185187 # 用来查找tag列表
186188 pattern_html_search_tag_list = compile ('<a href=".*?">(.*?)</a>' )
187189
190+ # 查找错误,例如 [错误,關鍵字過短,請至少輸入兩個字以上。]
191+ pattern_html_search_error = compile ('<fieldset>\n <legend>(.*?)</legend>\n <div class=.*?>\n (.*?)\n </div>\n </fieldset>' )
192+
188193 @classmethod
189194 def analyse_jm_search_html (cls , html : str ) -> JmSearchPage :
190- html = cls .pattern_html_search_shorten_for .search (html )[0 ]
195+ # 检查是否失败
196+ match = cls .pattern_html_search_error .search (html )
197+ if match is not None :
198+ topic , reason = match [1 ], match [2 ]
199+ JmModuleConfig .raise_regex_error_executor (f'{ topic } : { reason } ' , html )
200+
201+ # 缩小文本范围
202+ match = cls .pattern_html_search_shorten_for .search (html )
203+ if match is None :
204+ JmModuleConfig .raise_regex_error_executor ('未匹配到搜索结果' , html )
205+ html = match [0 ]
206+
207+ # 提取结果
191208 album_info_list = cls .pattern_html_search_album_info_list .findall (html )
192209
193210 for i , (album_id , title , * args ) in enumerate (album_info_list ):
0 commit comments