微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在 Python Beautifulsoup 中使用 itertools 将数据项数据添加到分组中

如何解决在 Python Beautifulsoup 中使用 itertools 将数据项数据添加到分组中

代码段已经在@Andrej Kesely 的帮助下工作,但需要一些改进。我无法理解如何在代码段中添加我想要的改进。

import requests
from bs4 import BeautifulSoup
from itertools import groupby
from time import sleep

url = "https://bscscan.com/tokentxns"
soup = BeautifulSoup(requests.get(url).content,"html.parser")

data = []
for tr in soup.select("tr:has(td)"):
    tds = [td.get_text(strip=True) for td in tr.select("td")]
    _,txn_hash,tm,age,from_,_,to_,value,token = tds
    a = tr.select("a")[-1]["href"][7:]
    data.append((a,token))

data = sorted(data)
for _,g in groupby(data,lambda k: k[0]):
    g = list(map(list,g))
    for subl in g[1:]:
        subl[0] = ""

    for subl in g:
        print("{:<45} {:<30} {:<20}".format(*subl) )
    print()
sleep(60)
    
        

电流输出

0xe9e7cea3dedca5984780bafc599bd69add087d56    1.8                   Binance-Peg ...(BUSD)
                                              112                   Binance-Peg ...(BUSD)
                                              64.5                  Binance-Peg ...(BUSD)
0xacfc95585d80ab62f67a14c566c1b7a49fe91167    36,659,510.5          FEGtoken (FEG)      
                                              6,667,905             FEGtoken (FEG)      
0xeb32ca0d96602176f177178a8c3a37fd557b6e5f    1,000,000             SpaceEthereu...(SPACEE...)
                                              1,951.2               SpaceEthereu...(SPACEE...)
                                              39.82                 SpaceEthereu...(SPACEE...)

想要的输出:TRANS / TOTAL(想要添加显示的数据)

-> 3 TRANS  0xe9e7cea3dedca5984780bafc599bd69add087d56  Binance-Peg ...(BUSD)      1.8            -> 178.3 TOTAL
                                                                                   112           
                                                                                   64.5          
-> 2 TRANS  0xacfc95585d80ab62f67a14c566c1b7a49fe91167  FEGtoken (FEG)             36,510.5   -> 43,327,415.5 TOTAL    
                                                                                   6,905           
-> 3 TRANS  0xeb32ca0d96602176f177178a8c3a37fd557b6e5f  SpaceEthereu...(SPACEE...) 1,000      -> 1,001,991.02 TOTAL
                                                                                   1,951.2       
                                                                                   39.82         

解决方法

试试:

import requests
from bs4 import BeautifulSoup
from itertools import groupby

url = "https://bscscan.com/tokentxns"
soup = BeautifulSoup(requests.get(url).content,"html.parser")

data = []
for tr in soup.select("tr:has(td)"):
    tds = [td.get_text(strip=True) for td in tr.select("td")]
    _,txn_hash,tm,age,from_,_,to_,value,token = tds
    a = tr.select("a")[-1]["href"][7:]
    data.append((a,token))

data = sorted(data)
for _,g in groupby(data,lambda k: k[0]):
    g = list(map(list,g))
    total = sum(float(s.replace(",","")) for _,s,*_ in g)
    total = [f"{total} TOTAL",*[""] * (len(g) - 1)]
    trans = [f"{len(g)} TRANS",*[""] * (len(g) - 1)]
    for subl in g[1:]:
        subl[0] = ""

    for tr,t,subl in zip(trans,total,g):
        print("{:<10} {:<45} {:<35} {:<30} {:<10}".format(tr,*subl,t))
    print()

打印:

1 TRANS    0x009cf7bc57584b7998236eff51b98a168dcea9b0    0                                   SyrupBar Tok...(SYRUP)         0.0 TOTAL 

5 TRANS    0x0e09fabb73bd3ade0a17ecc321fd13a19e81ce82    0                                   PancakeSwap ...(Cake)          29.477368351122603 TOTAL
                                                         0                                   PancakeSwap ...(Cake)                    
                                                         1.145747074846704429                PancakeSwap ...(Cake)                    
                                                         2.575601934206900007                PancakeSwap ...(Cake)                    
                                                         25.756019342069000075               PancakeSwap ...(Cake)                    

13 TRANS   0x154a9f9cbd3449ad22fdae23044319d6ef2a1fab    0                                   CryptoBlades...(SKILL)         34.27483551127292 TOTAL
                                                         0.002499999999999999                CryptoBlades...(SKILL)                   
                                                         0.025                               CryptoBlades...(SKILL)                   
                                                         0.029999999999999999                CryptoBlades...(SKILL)                   
                                                         0.032352133402011616                CryptoBlades...(SKILL)                   
                                                         0.04                                CryptoBlades...(SKILL)                   
                                                         0.14                                CryptoBlades...(SKILL)                   
                                                         0.17                                CryptoBlades...(SKILL)                   
                                                         0.176755050266694096                CryptoBlades...(SKILL)                   
                                                         0.3                                 CryptoBlades...(SKILL)                   
                                                         1.1                                 CryptoBlades...(SKILL)                   
                                                         1.281535918881612748                CryptoBlades...(SKILL)                   
                                                         30.976692408722603577               CryptoBlades...(SKILL)                   

1 TRANS    0x1796ae0b0fa4862485106a0de9b654efe301d0b2    1.723939371273915917                Polkamon (PMON)                1.723939371273916 TOTAL

1 TRANS    0x1b2016ccad83172f149f470457104e6659b411ab    9                                   PowerDEX (POWERD...)           9.0 TOTAL 


...

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?