From ffaea364635294f1f3163cd395fb2bedc57164a0 Mon Sep 17 00:00:00 2001 From: Yuukiy <76897913+Yuukiy@users.noreply.github.com> Date: Sat, 28 Aug 2021 13:01:58 +0800 Subject: [PATCH] =?UTF-8?q?javlib.py:=20=E8=A7=A3=E5=86=B3=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=E5=85=8D=E4=BB=A3=E7=90=86=E6=A8=A1=E5=BC=8F=E6=8A=93?= =?UTF-8?q?=E5=8F=96JavLib=E6=95=B0=E6=8D=AE=E6=97=B6=EF=BC=8C=E9=83=A8?= =?UTF-8?q?=E5=88=86=E6=83=85=E5=86=B5=E4=B8=8B=E7=AB=99=E7=82=B9=E9=87=8D?= =?UTF-8?q?=E5=AE=9A=E5=90=91=E9=94=99=E8=AF=AF=E5=AF=BC=E8=87=B4=E6=97=A0?= =?UTF-8?q?=E6=B3=95=E6=8A=93=E5=8F=96=E6=95=B0=E6=8D=AE=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- web/javlib.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/web/javlib.py b/web/javlib.py index 16dabef..d778ad0 100644 --- a/web/javlib.py +++ b/web/javlib.py @@ -2,6 +2,7 @@ import os import sys import logging +from urllib.parse import urlsplit, urljoin sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) @@ -21,10 +22,19 @@ else: # TODO: 发现JavLibrary支持使用cid搜索,会直接跳转到对应的影片页面,也许可以利用这个功能来做cid到dvdid的转换 def parse_data(movie: MovieInfo): """解析指定番号的影片数据""" + global base_url url = new_url = f'{base_url}/cn/vl_searchbyid.php?keyword={movie.dvdid}' html, resp = get_html(url, attach_raw=True, use_scraper=True) - if resp.history: # 如果仅有一个搜搜结果,JavLibrary会自动跳转 - new_url = resp.url + if resp.history: + if urlsplit(resp.url).netloc == urlsplit(base_url).netloc: + # 出现301重定向通常且新老地址netloc相同时,说明搜索到了影片且只有一个结果 + new_url = resp.url + else: + # 重定向到了不同的netloc时,新地址并不是影片地址。这种情况下新地址中丢失了path字段, + # 为无效地址(应该是JavBus重定向配置有问题),需要使用新的base_url抓取数据 + base_url = urljoin(resp.url, '/') + logger.info(f"请将配置文件中的JavLib防屏蔽地址更新为: {base_url}") + return parse_data(movie) else: # 如果有多个搜索结果则不会自动跳转,此时需要程序介入选择搜索结果 video_tags = html.xpath("//div[@class='video'][@id]/a") # 通常第一部影片就是我们要找的,但是以免万一还是遍历所有搜索结果 @@ -92,6 +102,7 @@ def parse_data(movie: MovieInfo): if __name__ == "__main__": + logger.setLevel(logging.DEBUG) movie = MovieInfo('STARS-213') parse_data(movie) print(movie)