gpt4 book ai didi

python - BeautifulSoup 抓取比特币价格问题

转载 作者:行者123 更新时间:2023-11-28 18:17:02 24 4
gpt4 key购买 nike

我对 python 还是个新手,尤其是 BeautifulSoup。几天来我一直在研究这些东西,玩弄一堆不同的代码并获得混合结果。然而,关于 this页面是我想抓取的比特币价格。价格位于: <span class="text-large2" data-currency-value="">$16,569.40</span>这意味着,我想让我的脚本只打印值所在的那一行。我当前的代码打印了整个页面,但看起来不太好,因为它打印了很多数据。有人可以帮助改进我的代码吗?

import requests
from BeautifulSoup import BeautifulSoup

url = 'https://coinmarketcap.com/currencies/bitcoin/'
response = requests.get(url)
html = response.content

soup = BeautifulSoup(html)
div = soup.find('text-large2', attrs={'class': 'stripe'})

for row in soup.findAll('div'):
for cell in row.findAll('tr'):
print cell.text

这是我在运行代码后得到的输出片段。它看起来不太好或可读性差。

#SourcePairVolume (24h)PriceVolume (%)Updated
1BitMEXBTC/USD$3,280,130,000$15930.0016.30%Recently
2BithumbBTC/KRW$2,200,380,000$17477.6010.94%Recently
3BitfinexBTC/USD$1,893,760,000$15677.009.41%Recently
4GDAXBTC/USD$1,057,230,000$16085.005.25%Recently
5bitFlyerBTC/JPY$636,896,000$17184.403.17%Recently
6CoinoneBTC/KRW$554,063,000$17803.502.75%Recently
7BitstampBTC/USD$385,450,000$15400.101.92%Recently
8GeminiBTC/USD$345,746,000$16151.001.72%Recently
9HitBTCBCH/BTC$305,554,000$15601.901.52%Recently

最佳答案

试试这个:

import requests
from BeautifulSoup import BeautifulSoup

url = 'https://coinmarketcap.com/currencies/bitcoin/'
response = requests.get(url)
html = response.content

soup = BeautifulSoup(html)
div = soup.find("div", {"class" : "col-xs-6 col-sm-8 col-md-4 text-left"
}).find("span", {"class" : "text-large2"})

for i in div:
print i

这会为我打印 16051.20。

稍后编辑:如果你把上面的代码放在一个函数中并循环它,它会不断更新。我现在得到了不同的值。

关于python - BeautifulSoup 抓取比特币价格问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47724574/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com