From 61a3063e99c751f589266220496067a6020b04af Mon Sep 17 00:00:00 2001 From: JayYoung <60842691+JayYoung2021@users.noreply.github.com> Date: Sat, 6 Feb 2021 20:03:07 +0800 Subject: [PATCH] 2020/2/6 Official site changed the xpath of activity status's display, and I followed. --- AwesomePU.py | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/AwesomePU.py b/AwesomePU.py index 2d3f56d..357879d 100644 --- a/AwesomePU.py +++ b/AwesomePU.py @@ -56,17 +56,18 @@ def show_activities(self) -> any: url = f'https://njtech.pocketuni.net/index.php?app=event&mod=School&act=board&cat=all&&p={p}' parent_html = etree.HTML(requests.get(url).text) for i in range(1, num_per_page + 1): - status = \ - parent_html.xpath(f'/html/body/div[2]/div/div[3]/div[2]/div[1]/ul/li[{i}]/div[3]/ul/li/text()')[0] - # if status not in ['活动已结束', '报名已结束']: - if True: + try: + status = parent_html.xpath( + f'/html/body/div[2]/div/div[3]/div[2]/div[1]/ul/li[{i}]/div[3]/ul/span/li/a/text()')[0] + except IndexError: + continue + if status not in ['活动已结束', '报名已结束']: rewards = parent_html.xpath( f'/html/body/div[2]/div/div[3]/div[2]/div[1]/ul/li[{i}]/div[2]/div[4]/text()') credit_hours = re.findall('\\d+', rewards[0])[0] if credit_hours != '0': - link = \ - parent_html.xpath( - f'/html/body/div[2]/div/div[3]/div[2]/div[1]/ul/li[{i}]/div[2]/div[1]/a/@href')[0] + link = parent_html.xpath( + f'/html/body/div[2]/div/div[3]/div[2]/div[1]/ul/li[{i}]/div[2]/div[1]/a/@href')[0] sub_html = etree.HTML(self.session.get(link).text) content = sub_html.xpath('string(/html/body/div[1]/div[3]/div/div/div[1]/div[2])') if re.search('可参与部落:(.*)', content) is None: @@ -82,13 +83,15 @@ def show_activities(self) -> any: if no_activity: no_activity = False title = parent_html.xpath( - f'/html/body/div[2]/div/div[3]/div[2]/div[1]/ul/li[{i}]/div[2]/div[1]/a/text()') + f'/html/body/div[2]/div/div[3]/div[2]/div[1]/ul/li[{i}]/div[2]/div[' + f'1]/a/text()') print(f'活动名称:{unescape(title[0])}') print(f'实践学时:{credit_hours}') location = \ - sub_html.xpath('/html/body/div[1]/div[3]/div/div/div[1]/div[2]/a[1]/text()')[0] + sub_html.xpath( + '/html/body/div[1]/div[3]/div/div/div[1]/div[2]/a[1]/text()')[0] print(f'活动地点:{location}') time = re.search('活动时间:(.*)', content).group(1)