如何在 python 和 beautifulsoup 中的文本文件中将多个 for 循环打印结果写入一行
How to write multiple for loop print results into a single line in a text file in python and beautifulsoup
下面的代码片段可以正常工作,但不是很像样。我不知道如何将显示的内容写入 outputfile.txt 文件。我希望代码段将显示的内容写入文本文件。
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
由于输出中的第一个值是一个列表,因此您可以 join
值使其成为单个字符串。如果你想将这两个值放在单独的变量中,那么你可以再次 split
它们并按照你的意愿存储它。现在,由于您只是写入 txt
,您可以将最后一个打印语句保存在一个变量中,然后直接将其写入文件。
我对上面的代码做了 2 处更改:
- 使用了
join
命令
- 更改了
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
这是我得到的输出:
一种更简单的可能方法:
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
等等
下面的代码片段可以正常工作,但不是很像样。我不知道如何将显示的内容写入 outputfile.txt 文件。我希望代码段将显示的内容写入文本文件。
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
由于输出中的第一个值是一个列表,因此您可以 join
值使其成为单个字符串。如果你想将这两个值放在单独的变量中,那么你可以再次 split
它们并按照你的意愿存储它。现在,由于您只是写入 txt
,您可以将最后一个打印语句保存在一个变量中,然后直接将其写入文件。
我对上面的代码做了 2 处更改:
- 使用了
join
命令 - 更改了
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
这是我得到的输出:
一种更简单的可能方法:
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
等等