import requests
import pandas as pd
url = "https://coinmarketcap.com/new/"
page = requests.get(url,headers={'User-Agent': 'Mozilla/5.0'}, timeout=1)
pagedata = page.text
usecols = ["Name", "Price", "1h", "24h", "MarketCap", "Volume"]#, "Blockchain"]
df = pd.read_html(pagedata)[0] #Checking table
df[["Name", "Symbol"]] = df["Name"].str.split(r"\d+", expand=True)
df = df.rename(columns={"Fully Diluted Market Cap": "MarketCap"})[usecols]
dfAsString = df.to_string(index=False)
print(dfAsString)
当前代码输出:(已截断)
Name Price 1h 24h MarketCap Volume
0 DollarPepe $0.02752 22.64% 336.25% $3 $456,913
1 Billy Token $0.00002822 41.69% 75.80% $1,958,942 $6,999,241
2 JEFF $0.1946 4.42% 226.18% $19,458,328 $19,744,583
3 PUG AI $0.00000001459 10.80% 15.84% $1,459,428 $239,454
4 FART COIN $0.0000004281 1.13% 42.13% $42,806,075 $46,604
[30 rows x 6 columns]
如何根据特定列(24小时)生成排序后的输出?-> 截断
Name Price 1h 24h MarketCap Volume
0 DollarPepe $0.02752 22.64% 336.25% $3 $456,913
2 JEFF $0.1946 4.42% 226.18% $19,458,328 $19,744,583
1 Billy Token $0.00002822 41.69% 75.80% $1,958,942 $6,999,241
4 FART COIN $0.0000004281 1.13% 42.13% $42,806,075 $46,604
3 PUG AI $0.00000001459 10.80% 15.84% $1,459,428 $239,454
[30 rows x 6 columns]