Skip to content

Commit

Permalink
feat: 基于正则表达式解析用户总资产数据
Browse files Browse the repository at this point in the history
  • Loading branch information
FHU-yezi committed Mar 26, 2024
1 parent 147200e commit 9a21b72
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 9 deletions.
6 changes: 2 additions & 4 deletions jkit/_network_request.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from typing import Any, Dict, Optional

from lxml.html import HtmlElement
from lxml.html import fromstring as parse_html
from msgspec.json import Decoder

from jkit.config import CONFIG
Expand Down Expand Up @@ -52,8 +50,8 @@ async def get_html(
*,
endpoint: str,
path: str,
) -> HtmlElement:
) -> str:
response = await HTTP_CLIENT.get(f"{endpoint}{path}")
response.raise_for_status()

return parse_html(response.content)
return response.text
10 changes: 5 additions & 5 deletions jkit/user.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from enum import Enum
from re import compile as re_compile
from typing import (
TYPE_CHECKING,
Any,
Expand Down Expand Up @@ -43,6 +44,8 @@
from jkit.collection import Collection
from jkit.notebook import Notebook

ASSETS_AMOUNT_REGEX = re_compile(r"收获喜欢[\s\S]*?<p>(.*)</p>[\s\S]*?总资产")


class UserBadge(DataObject, **DATA_OBJECT_CONFIG):
name: NonEmptyStr
Expand Down Expand Up @@ -255,11 +258,8 @@ async def assets_amount(self) -> float:
data = await get_html(endpoint=CONFIG.endpoints.jianshu, path=f"/u/{self.slug}")

try:
return float(
data.xpath("//div[@class='meta-block']/p")[2]
.text.replace(".", "")
.replace("w", "000")
)
assets_amount: str = ASSETS_AMOUNT_REGEX.findall(data)[0]
return float(assets_amount.replace(".", "").replace("w", "000"))
except IndexError:
raise APIUnsupportedError(
"受 API 限制,无法获取此用户的资产量信息"
Expand Down

0 comments on commit 9a21b72

Please sign in to comment.