From 2da0ef0959e50544ff4fe2b50ceb66f34dde535f Mon Sep 17 00:00:00 2001 From: lowking <33308659+lowking@users.noreply.github.com> Date: Tue, 2 Nov 2021 23:43:39 +0800 Subject: [PATCH] =?UTF-8?q?pl=20=E5=A2=9E=E5=8A=A0api=E8=B6=85=E6=97=B6?= =?UTF-8?q?=E6=8F=90=E7=A4=BA=E4=BB=A5=E5=8F=8A=E4=BB=A3=E7=A0=81=E9=80=BB?= =?UTF-8?q?=E8=BE=91=E4=BC=98=E5=8C=96=20(#193)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- list.json | 2 +- pl.py | 18 ++++++++++++------ 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/list.json b/list.json index 5e9e3bb..0edc891 100644 --- a/list.json +++ b/list.json @@ -482,7 +482,7 @@ }, { "name": "pl", - "version": "1.02", + "version": "1.03", "section": "chat", "maintainer": "lowking", "size": "4.0 kb", diff --git a/pl.py b/pl.py index 64a2161..2f4e144 100644 --- a/pl.py +++ b/pl.py @@ -26,36 +26,42 @@ async def pl(context): await context.edit(f"请先安装依赖:\n`{executable} -m pip install bs4`\n随后,请重启 pagermaid。") return action = context.arguments.split() + status = False if len(action) == 1: await context.edit("查询中 . . .") - status = False st = action[0] st = st.encode('gb2312') m = {'tj_so': st, } s = parse.urlencode(m) - for _ in range(3): # 最多重试3次 + for _ in range(2): # 最多重试2次 try: - plhtml = get(f"http://www.gd2063.com/pl/?{s}") + try: + plhtml = get(f"http://www.gd2063.com/pl/?{s}", timeout=5) + except: + await context.edit("api请求异常,请访问\nhttp://www.gd2063.com/\n查看是否正常") + status = True + break htmlStr = plhtml.content.decode("gbk") soup = BeautifulSoup(htmlStr, 'html.parser') arr = soup.find_all(name='a', attrs={"class": "heise"}, limit=10) result = "" for a in arr: - if (a.text != None): + if a.text is not None and a.text != "": txt = a.text.replace("嘌呤含量", "➟ ") result = f"{result}{txt}\n" - status = True if result == "": await context.edit("没有查到结果") else: await context.edit(result) + status = True break except: + await sleep(5) pass if not status: - await context.edit(f"呜呜呜,试了3次都没查到呢") + await context.edit(f"呜呜呜,试了2次都没查到呢") else: await context.edit(f"乱写什么东西呀!格式如下:\n" f"【-pl 食物名】查询食物嘌呤含量")