Commit 5ba58040 authored by 陈涛's avatar 陈涛

修改解析beacon

parent 0d260465
......@@ -53,3 +53,4 @@ class StakingFundNav(StakingFund):
default_factory=lambda: datetime.datetime.utcnow().replace(hour=0, minute=0, second=0, microsecond=0, tzinfo=pytz.UTC),
description="记录时间"
)
node_income_assets: Dict[str, float] = Field(default={}, description='质押资产')
\ No newline at end of file
......@@ -174,29 +174,31 @@ class BeaconChaService:
return eval(re.findall(r'var executionIncomeHistory = \[(.*?)\]', response.text)[0])
def get_rewards(self, index_or_pubkey: Union[int, str]) -> Rewards:
total_reward_element_path = "/html/body/main/div[1]/div[2]/div[2]/div/table/tbody/tr[1]/td/span"
total_reward_element_text_path = "/html/body/main/div[1]/div[2]/div[2]/div/table/tbody/tr[1]/td/span"
income_today_xpath = "/html/body/main/div[1]/div[2]/div[2]/div/table/tbody/tr[2]/td/span"
income_xpath = "/html/body/main/div[1]/div[2]/div[2]/div/table/tbody/tr[3]/td/span"
income_today_total_path = "/html/body/main/div[1]/div[2]/div[2]/div/table/tbody/tr[2]/td/span/span"
total_income1d_xpath = "/html/body/main/div[1]/div[2]/div[2]/div/table/tbody/tr[3]/td/span/span[1]"
total_income7d_xpath = "/html/body/main/div[1]/div[2]/div[2]/div/table/tbody/tr[3]/td/span/span[2]"
total_income365d_xpath = "/html/body/main/div[1]/div[2]/div[2]/div/table/tbody/tr[3]/td/span/span[3]"
response = requests.get(f"{self.base_url}/validator/{index_or_pubkey}")
html = response.content
# 解析 HTML 页面
tree = etree.HTML(html)
total_rewards_re = tree.xpath('/html/body/main/div[1]/div[2]/div[2]/div/table/tbody/tr[1]/td/span')[0]
total_rewards_re = tree.xpath(total_reward_element_path)[0]
total_rewards_title = total_rewards_re.get("title")
total_list = re.findall(r'<span>(.?\d+\.?\d*) ETH</span>', total_rewards_title)
total_total_rewards_text = tree.xpath('/html/body/main/div[1]/div[2]/div[2]/div/table/tbody/tr[1]/td/span/b')[0].text
total_total_rewards = re.findall(r".?\d+\.?\d*", total_total_rewards_text)[0]
total_list = re.findall(r'<span>(.?\d+\.?\d*) ETH</span>', total_rewards_title) if total_rewards_title else [0, 0]
total_total_rewards_text = etree.tostring(tree.xpath(total_reward_element_text_path)[0], encoding='unicode')
total_total_rewards = re.findall(r">(.?\d+\.?\d*)", total_total_rewards_text)[0]
# income_today
income_today_xpath = "/html/body/main/div[1]/div[2]/div[2]/div/table/tbody/tr[2]/td/span"
income_today_total_path = "/html/body/main/div[1]/div[2]/div[2]/div/table/tbody/tr[2]/td/span/span/b"
income_today_re, total_income_today = tree.xpath(income_today_xpath)[0], tree.xpath(income_today_total_path)[0].text
income_today_re, total_income_today = tree.xpath(income_today_xpath)[0], etree.tostring(tree.xpath(income_today_total_path)[0], encoding='unicode')
income_today_list = re.findall(r"(.?\d+\.?\d*) ETH", income_today_re.get("title"))
total_income_today = re.findall(r".?\d+\.?\d*", total_income_today)[0]
total_income_today = re.findall(r">(.?\d+\.?\d*)", total_income_today)[0]
# income
total_income1d_xpath = "/html/body/main/div[1]/div[2]/div[2]/div/table/tbody/tr[3]/td/span/span[1]/b"
total_income7d_xpath = "/html/body/main/div[1]/div[2]/div[2]/div/table/tbody/tr[3]/td/span/span[2]/b"
total_income365d_xpath = "/html/body/main/div[1]/div[2]/div[2]/div/table/tbody/tr[3]/td/span/span[3]/b"
total_income1d = re.findall(r".?\d+\.?\d*", tree.xpath(total_income1d_xpath)[0].text)[0]
total_income7d = re.findall(r".?\d+\.?\d*", tree.xpath(total_income7d_xpath)[0].text)[0]
total_income31d = re.findall(r".?\d+\.?\d*", tree.xpath(total_income365d_xpath)[0].text)[0]
income_xpath = "/html/body/main/div[1]/div[2]/div[2]/div/table/tbody/tr[3]/td/span"
total_income1d = re.findall(r">(.?\d+\.?\d*)", etree.tostring(tree.xpath(total_income1d_xpath)[0], encoding='unicode'))[0]
total_income7d = re.findall(r">(.?\d+\.?\d*)", etree.tostring(tree.xpath(total_income7d_xpath)[0], encoding='unicode'))[0]
total_income31d = re.findall(r">(.?\d+\.?\d*)", etree.tostring(tree.xpath(total_income365d_xpath)[0], encoding='unicode'))[0]
income_list = re.findall(r".?\d+\.?\d*", tree.xpath(income_xpath)[0].get("title"))
result = Rewards(
total_rewards=NodeGeneral(cl=float(total_list[0]), el=float(total_list[1]), total=float(total_total_rewards)),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment