评估后如何在 python 中的循环内移动某些数据部分

How to move some parts of data inside a loop in python after an evaluation

我一直在想办法用我抓取的数据来完成我想要的事情。我的问题是如何移动这些数据的某些部分以提高格式化可读性。

from bs4 import BeautifulSoup
from urllib import request
from urllib.request import Request, urlopen
addrlist = ['0x13258c89cc8f640ff68d23f1d31d38a4c4a4376a6120ca077f66a0b337912386',
            '0x252b25b7c0ce8ebfbb85824a7af413c95ac91f544c3ae34a23c6390c6baec5f7',
            '0x5b3ab3c8765a2a55ad5ac394f5f89e150fdb5ea3c4620b8e8a6c9be10ab53557',
            '0x9c4d1308d85f9e175809b8a49418ff6cf72264bca10660cc8d91a01b93487503']
url = "https://bscscan.com/tx/"
count = 0
for i in addrlist:
    count += 1
    url1 = str(url) + str(i)
    req = Request(url1, headers={'User-Agent': 'Mozilla/5.0'})
    webpage = urlopen(req).read()
    soup = BeautifulSoup(webpage, 'html.parser')        
    main_data = soup.select("ul#wrapperContent div.media-body")
    print("Item:", count)
    for item in main_data:
        all_span = item.find_all("span", class_='mr-1')
        last_span = all_span[-1]
        all_a = item.find_all("a")
        last_a = all_a[-1]
        tval = last_span.get_text(strip=True)
        if (last_a.get_text(strip=True)[:11]) == "Wrapped BNB": 
            print (str(tval) + "   " + str(last_a.get_text(strip=True)[:11]))
        elif (last_a.get_text(strip=True)[:11]) == "Binance-Peg":
            print (str(tval) + "   " + str(last_a.get_text(strip=True)[:11]))
        print("       ** {:>35} | {:14} | {}".format(last_a['href'][7:],  last_a.get_text(strip=True)[:11], last_span.get_text(strip=True)))
    print ("--------------------------------------------------------------------------------------------------------------")

当前输出:(部分)

Item: 1
       ** 0x330f4fe5ef44b4d0742fe8bed8ca5e29359870df | Jade Curren    | 23 (.32)
       ** 0x0e09fabb73bd3ade0a17ecc321fd13a19e81ce82 | PancakeSwap    | 0.34252105736058847 (.64)
0.017788594726254879 (.83)   Wrapped BNB                            #-- part I need to move on top
       ** 0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c | Wrapped BNB    | 0.017788594726254879 (.83)
--------------------------------------------------------------------------------------------------------------
Item: 2
1,000 (7.66)   Binance-Peg                                         #-- part I need to move on top
       ** 0x55d398326f99059ff775485246999027b3197955 | Binance-Peg    | 1,000 (7.66)
1.983650833673009387 (4.49)   Wrapped BNB                          #-- part I need to move
       ** 0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c | Wrapped BNB    | 1.983650833673009387 (4.49)
       ** 0xd7730681b1dc8f6f969166b29d8a5ea8568616a3 | Nafter (NAF    | 7,557.363316600945826765
--------------------------------------------------------------------------------------------------------------

想要的输出: 移动了找到的数据(Wrapped BNB 和 Binance-Peg)

Item: 1  -> 0.017788594726254879 (.83)   Wrapped BNB   #--- moved data
       ** 0x330f4fe5ef44b4d0742fe8bed8ca5e29359870df | Jade Curren    | 23 (.32)
       ** 0x0e09fabb73bd3ade0a17ecc321fd13a19e81ce82 | PancakeSwap    | 0.34252105736058847 (.64)
       ** 0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c | Wrapped BNB    | 0.017788594726254879 (.83)
--------------------------------------------------------------------------------------------------------------
Item: 2  -> 1,000 (7.66)   Binance-Peg / 1.983650833673009387 (4.49)   Wrapped BNB  #--- moved data
       ** 0x55d398326f99059ff775485246999027b3197955 | Binance-Peg    | 1,000 (7.66)
       ** 0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c | Wrapped BNB    | 1.983650833673009387 (4.49)
       ** 0xd7730681b1dc8f6f969166b29d8a5ea8568616a3 | Nafter (NAF    | 7,557.363316600945826765
--------------------------------------------------------------------------------------------------------------
Item: 3  -> 1,602.702116053853306849 (,603.21)   Binance-Peg / 3.34475130349510875 (,660.01)   Wrapped BNB  #--- moved data
       ** 0xe9e7cea3dedca5984780bafc599bd69add087d56 | Binance-Peg    | 1,602.702116053853306849 (,603.21)    
       ** 0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c | Wrapped BNB    | 3.34475130349510875 (,660.01)
       ** 0x04c747b40be4d535fc83d09939fb0f626f32800b | ITAM (ITAM)    | 30,451.541477620588584374 (,358.26)
--------------------------------------------------------------------------------------------------------------
Item: 4  -> 90.381529911617319063 (.41)   Binance-Peg  #--- moved data
       ** 0x762539b45a1dcce3d36d080f74d1aed37844b878 | Linear Toke    | 1,438.067022011971766044 (.12)
       ** 0xe9e7cea3dedca5984780bafc599bd69add087d56 | Binance-Peg    | 90.381529911617319063 (.41)
--------------------------------------------------------------------------------------------------------------

如果您希望输出中的数据顺序与输入数据中的数据顺序不同,那么您需要先构建数据缓冲区,然后才能打印它。参考下面的代码。

from urllib import request
from bs4 import BeautifulSoup
from urllib.request import Request, urlopen

addrlist = ['0x13258c89cc8f640ff68d23f1d31d38a4c4a4376a6120ca077f66a0b337912386',
            '0x252b25b7c0ce8ebfbb85824a7af413c95ac91f544c3ae34a23c6390c6baec5f7',
            '0x5b3ab3c8765a2a55ad5ac394f5f89e150fdb5ea3c4620b8e8a6c9be10ab53557',
            '0x9c4d1308d85f9e175809b8a49418ff6cf72264bca10660cc8d91a01b93487503']
url = "https://bscscan.com/tx/"
count = 0

for i in addrlist:

    count += 1
    url1 = str(url) + str(i)
    req = Request(url1, headers={'User-Agent': 'Mozilla/5.0'})
    webpage = urlopen(req).read()
    soup = BeautifulSoup(webpage, 'html.parser')
    main_data = soup.select("ul#wrapperContent div.media-body")

    output = [f"Item: {count} -> "]
    binancePeg = False

    for item in main_data:

        all_span = item.find_all("span", class_='mr-1')
        last_span = all_span[-1]
        all_a = item.find_all("a")
        last_a = all_a[-1]
        tval = last_span.get_text(strip=True)

        substr = last_a.get_text(strip=True)[:11]
        if substr == "Wrapped BNB":
            if binancePeg == True: output[0] += " / "
            output[0] += tval + "   " + substr
        elif substr == "Binance-Peg":
            output[0] += tval + "   " + substr
            binancePeg = True

        output.append("       ** {:>35} | {:14} | {}".format(last_a['href'][7:],
            last_a.get_text(strip=True)[:11], last_span.get_text(strip=True)))

    print("\n".join(output), "\n", "-"*100)