【问题标题】:How to write multiple for loop print results into a single line in a text file in python and beautifulsoup如何在python和beautifulsoup的文本文件中将多个for循环打印结果写入一行
【发布时间】:2021-08-11 11:55:14
【问题描述】:

下面的 sn-p 工作正常,但不是很像样。我不知道如何将显示的内容写入 outputfile.txt 文件。我希望 sn-p 将显示的内容写入文本文件。

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, value, token))

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

    for tr, t, subl in zip(trans, total, g):
        print ("\t\t" + str(tr) + "   "  + str(t))

当前输出:保存在 outputfile.txt 中 - 无法呈现

['0x088bebef4e371757509e64d3508b6da6f376e2ac', '', 'DrakeBall To...(DBall)']    
['0x0e09fabb73bd3ade0a17ecc321fd13a19e81ce82', '', 'PancakeSwap ...(Cake)']    
['0x154a9f9cbd3449ad22fdae23044319d6ef2a1fab', '', 'CryptoBlades...(SKILL)']    
['0x3621f5b9786dfa52759c0392a72ac6818ed2c84f', '', 'EQIFI (EQX)']  1 TRANS  182513805147.0 TOTAL
['0x363621cb1b32590c55f283432d91530d77cf532f', '', 'BABYCAKE_Div...(BABYCA...)']  1 TRANS  37840.385 TOTAL
['0x4fd6d315bef387fad2322fbc64368fc443f0886d', '', 'Pancake LPs (Cake-L...)']    

想要的输出写入 outputfile.txt:

0x9c65ab58d8d978db963e63f2bfb7121627e3a739  MDX Token (MDX)         1 TRANS   97.10128249433292 TOTAL
0xacb8f52dc63bb752a51186d1c55868adbffee9c1  BunnyPark (BP)          3 TRANS   340.2936687126161 TOTAL
0xb7dba4c673bedb174dc3ff7ec65d17c863d39b16  FAT CAKE (FATCAK...)    2 TRANS   6408272.9511043355 TOTAL
0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c  Wrapped BNB (WBNB)      2 TRANS   0.4472397706686812 TOTAL
0xdae18b46e0dbfecd441c47de1a6d7b57455a83ee  Pancake LPs (Cake-L...) 1 TRANS   0.22360679774997796 TOTAL

【问题讨论】:

    标签: python python-3.x beautifulsoup text-files itertools


    【解决方案1】:

    一种更简单的可能方法:

    tab = soup.select_one('table')
    data = []
    for entry in tab.select('table tbody tr'):
        cells = entry.select('td')
        token = cells[-1].select_one('a')['href'].split('token/')[1]
        data.append([cells[-1].text,cells[-2].text,token])
    
    for k, g in groupby(sorted(data), lambda k: k[0]):
        g = list(map(list, g))
        tot = sum([float(i[1].replace(',','')) for i in g ])    
        print(k,',',g[0][2],',',len(g),"TRANS",',',tot,'TOTAL')
    

    在我运行它的时候,输出是这样的:

     BABY DOGE BI... (BABYDB) , 0x6d9fb3332f62fc044d5075feeea597a92f1ce0ad , 1 TRANS , 60634807618.46239 TOTAL
     BLADE KNIGHT (BK) , 0x2bfe217caa076027ac24af0e261ed311478ddf78 , 1 TRANS , 4.800279067385619 TOTAL
     Binance-Peg ... (BSC-US...) , 0x55d398326f99059ff775485246999027b3197955 , 1 TRANS , 15.242784023721224 TOTAL
    

    等等

    【讨论】:

    • 这非常棒且很有帮助。它有效且非常简单。谢谢。
    【解决方案2】:

    由于输出中的第一个值是一个列表,因此您只需 join 将这些值设为单个字符串。如果您希望这两个值在单独的变量中,那么您可以再次split 它们并根据需要存储它。现在,由于您只是写入txt,因此您可以将最后一个打印语句保存在变量中并直接将其写入文件中。

    我对上面的代码进行了 2 处更改:

    1. 使用join 命令
    2. 更改了 subl 的打印位置,并通过乘以空格来对齐该打印语句。
    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, value, token))
    
    data = sorted(data)
    
    for _, g in groupby(data, lambda k: k[2]):
        g = list(map(list, g))
        trans = [f"{len(g)} TRANS", *[""] * (len(g) - 1)]
        total = sum(float(s.replace(",", "")) for _, s, *_ in g)
        total = [f"{total} TOTAL", *[""] * (len(g) - 1)]
        
        for subl in g[0:]:
            subl[1] = ""
            subl = ' '.join(map(str, subl))
    
        g = [' '.join(i) for i in g]        # join to change the 2D list to 1D
        i =0
        for tr, t, subl in zip(trans, total, g):
            if i ==0:    # removes duplicates in the for loop
                # multiplying the space value based on the max len of the values of lsit g
                print (subl + ' '* (70-len(subl)) +  "\t " + str(tr) + "\t "  + str(t))
                i+=1
        print()
    
    # 70 is max length a value inside list g had so I am subtracting it with current values len
    # to make the spaces next column start at the same point
    

    这是我得到的输出:

    【讨论】:

    • 很好的解释和非常有用的想法。谢谢
    猜你喜欢
    • 2016-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-23
    • 1970-01-01
    • 2015-08-12
    • 1970-01-01
    相关资源
    最近更新 更多