更新代码

This commit is contained in:
JoeamAmier 2023-08-27 10:36:05 +08:00
parent 490788904a
commit b7bb3ca05c
2 changed files with 28 additions and 23 deletions

View File

@ -9,24 +9,24 @@ class Explore:
time_format = "%Y-%m-%d %H:%M:%S"
def run(self, html: str) -> dict:
data = self.get_json_data(html)
return self.extract_data(data)
data = self.__get_json_data(html)
return self.__extract_data(data)
def get_json_data(self, html: str) -> dict:
def __get_json_data(self, html: str) -> dict:
data = self.explore_data.findall(html)
return {} if len(data) != 1 else loads(data[0])
def extract_data(self, data: dict) -> dict:
def __extract_data(self, data: dict) -> dict:
result = {}
self.extract_interact_info(result, data)
self.extract_tags(result, data)
self.extract_info(result, data)
self.extract_time(result, data)
self.extract_user(result, data)
self.__extract_interact_info(result, data)
self.__extract_tags(result, data)
self.__extract_info(result, data)
self.__extract_time(result, data)
self.__extract_user(result, data)
return result
@staticmethod
def extract_interact_info(container: dict, data: dict):
def __extract_interact_info(container: dict, data: dict):
interact_info = data["interactInfo"]
container["收藏数量"] = interact_info["collectedCount"]
container["评论数量"] = interact_info["commentCount"]
@ -34,24 +34,24 @@ class Explore:
container["点赞数量"] = interact_info["likedCount"]
@staticmethod
def extract_tags(container: dict, data: dict):
def __extract_tags(container: dict, data: dict):
tags = data["tagList"]
container["作品标签"] = [i["name"] for i in tags]
@staticmethod
def extract_info(container: dict, data: dict):
def __extract_info(container: dict, data: dict):
container["作品ID"] = data["noteId"]
container["作品标题"] = data["title"]
container["作品描述"] = data["desc"]
container["作品类型"] = {"video": "视频", "normal": "图文"}[data["type"]]
def extract_time(self, container: dict, data: dict):
def __extract_time(self, container: dict, data: dict):
container["发布时间"] = strftime(self.time_format, data["time"])
container["最后更新时间"] = strftime(
self.time_format, data["lastUpdateTime"])
@staticmethod
def extract_user(container: dict, data: dict):
def __extract_user(container: dict, data: dict):
user = data["user"]
container["作者昵称"] = user["nickname"]
container["作者ID"] = user["userId"]

View File

@ -30,20 +30,25 @@ class XHS:
if cookie:
self.headers["Cookie"] = cookie
def get_image(self, url: str, download=False):
urls = self.image.get_image_link(url)
def get_image(self, container: dict, html: str, download):
urls = self.image.get_image_link(html)
if download:
self.download.run(urls)
return urls
container["下载地址"] = urls
def get_video(self, url: str, download=False):
url = self.video.get_video_link(url)
def get_video(self, container: dict, html: str, download):
url = self.video.get_video_link(html)
if download:
self.download.run([url])
return url
container["下载地址"] = url
def extract(self, url: str):
def extract(self, url: str, download=False) -> dict:
html = self.html.get_html(url)
if not html:
return None
self.explore.run(html)
return {}
data = self.explore.run(html)
if data["作品类型"] == "视频":
self.get_video(data, html, download)
else:
self.get_image(data, html, download)
return data