BeautifulSoup报废比特币价格问题 [英] BeautifulSoup scraping Bitcoin Price issue

查看:99
本文介绍了BeautifulSoup报废比特币价格问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还是python新手,尤其是BeautifulSoup.我已经阅读了几天这些内容,并尝试了许多不同的代码并获得了混合结果.但是,在页面上,这是我要抓取的比特币价格.价格位于: <span class="text-large2" data-currency-value="">$16,569.40</span> 就是说,我想让我的脚本只打印该值所在的那一行.我当前的代码可以打印整个页面,但它看起来并不好看,因为它正在打印大量数据.有人可以帮助改善我的代码吗?

I am still new to python, and especially BeautifulSoup. I've been reading up on this stuff for a few days and playing around with bunch of different codes and getting mix results. However, on this page is the Bitcoin Price I would like to scrape. The price is located in: <span class="text-large2" data-currency-value="">$16,569.40</span> Meaning that, I'd like to have my script print only that line where the value is. My current code prints the whole page and it doesn't look very nice, since it's printing a lot of data. Could anybody please help to improve my code?

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

这是运行代码后得到的输出的一部分.它看起来不太好看也不可读.

And this is a snip of the output I get after running the code. It doesn't look very nice or readable.

#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.

This prints 16051.20 for me.

稍后如果将以上代码放入函数中并使其循环,它将不断更新.我现在得到了不同的值.

Later and if you put the above code in a function and loop it it will constantly update. I get different values now.

这篇关于BeautifulSoup报废比特币价格问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆