Python BeautifulSoup刮削比特币价格问题

Python BeautifulSoup刮削比特币价格问题,python,beautifulsoup,Python,Beautifulsoup,我对python还是新手,尤其是BeautifulSoup。我已经读了几天这方面的东西,玩了一堆不同的代码,得到了混合的结果。然而,第页是我想要的比特币价格。价格位于: 16569.40美元 也就是说,我想让脚本只打印值所在的行。我当前的代码打印整个页面,但它看起来不是很好,因为它打印了很多数据。有人能帮我改进代码吗 import requests from BeautifulSoup import BeautifulSoup url = 'https://coinmarketcap.com/

我对python还是新手,尤其是BeautifulSoup。我已经读了几天这方面的东西,玩了一堆不同的代码,得到了混合的结果。然而,第页是我想要的比特币价格。价格位于:
16569.40美元
也就是说,我想让脚本只打印值所在的行。我当前的代码打印整个页面,但它看起来不是很好,因为它打印了很多数据。有人能帮我改进代码吗

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


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

这很有效。但我认为您使用的是较旧版本的BeautifulSoup,请尝试在命令提示符或PowerShell中安装bs4

import requests
from bs4 import BeautifulSoup

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

soup = BeautifulSoup(html, 'html.parser')

value = soup.find('span', {'class': 'text-large2'})
print(''.join(value.stripped_strings))