【发布时间】:2021-07-29 23:44:58
【问题描述】:
我目前正在努力创建一个从https://coinmarketcap.com 上的表中抓取数据的程序。我看到我有点过头了。但是,我正在尝试了解如何能够自己完成这一切。到目前为止,我的程序打印了加密货币排名、名称和股票代码。现在,我正在努力从表格中抓取动态变化的价格。这是我的代码:
import requests
from bs4 import BeautifulSoup
url = "https://coinmarketcap.com"
soup = BeautifulSoup(requests.get(url).content, "html.parser")
rank = 1
for td in soup.select("td:nth-of-type(3)"):
t = " ".join(tag.text for tag in td.select("p, span")).strip()
print(rank, "|", end =" "); print("{:<30} {:<10}".format(*t.rsplit(maxsplit=1)))
rank = rank + 1
for td in soup.select("td:nth-of-type(4)"):
t = " ".join(tag.text for tag in td.select("a")).strip()
print("{}_1d".format(t.rsplit(maxsplit=1)))
打印如下:
1 | Bitcoin BTC
[]_1d
2 | Ethereum ETH
[]_1d
3 | Tether USDT
[]_1d
4 | Binance Coin BNB
[]_1d
and so on...
如何让它打印加密货币的当前价格而不仅仅是文字?我可以自己弄清楚格式,只需要帮助显示实际数据。任何帮助是极大的赞赏。如果您能解释您的解决方案,那将更有帮助。
【问题讨论】:
标签: python beautifulsoup html-table cryptocurrency inspect