1

我正在构建一个小模块,它的任务是执行以下操作: - 阅读网页(cavirtex.com/orderbook) - 使用 urllib 获取源代码并使用 beautifulsoup 打开它 - 解析并获取body.div(id='xx') 现在,我被卡住了这里。我想重新美化结果并在较大的 tr 内迭代两行 td 并获取值并将它们求和。如果有人知道该怎么做,请向我解释一下,因为我已经被困在这里好几个小时了。哦,这是我的源代码:

myurl = urllib.urlopen('http://cavirtex.com/orderbook').read()
soup = BeautifulSoup(myurl)

selling = soup.body.div(id ='orderbook_buy') 
selling = str(selling)
selling = BeautifulSoup(selling)
Sresult = selling.find_all(['tr'])
amount = 30
count = 0
cadtot = 0
locamount = 0
for rows in Sresult:
    #agarrar string especifico para vez
    Wresult = Sresult[count]
    #crear lista
    Eresult = [Wresult]
    Eresult = str(Eresult)
    cosito = str(Eresult[count])

    print cosito
    count = int(count) + 1
    cadtot = cadtot + locamount
4

2 回答 2

2

不是直接回答您的问题,但如果您的目标是从 下载和处理订单簿cavirtex.com,我建议您改用图表 API:

https://www.cavirtex.com/api/CAD/orderbook.json

该链接以友好的 JSON 格式包含您需要的所有信息。

例子:

import urllib
import json

url = "https://www.cavirtex.com/api/CAD/orderbook.json"
orderbook_json = urllib.urlopen(url).read()
orderbook = json.loads(orderbook_json)

print(orderbook['bids'])
print(orderbook['asks'])

还有:

https://www.cavirtex.com/api/CAD/trades.json

大多数比特币交易所都支持相同的 API,如 bitcoincharts.com 所述:http: //bitcoincharts.com/about/exchanges/

享受!

于 2013-07-19T18:53:35.100 回答
0

你需要把这张表中的价格和价值相加吗?这就是这样做的,并将结果存储为字典,其中从 1 到 21 的整数作为键,并将 CAD 中的价格和价值的总和作为值。如果您需要一些其他输出,您可以轻松调整它以满足您的特定需求。

import urllib
from bs4 import *

myurl = urllib.urlopen('http://cavirtex.com/orderbook').read()
soup = BeautifulSoup(myurl)

selling = soup.body.div(id ='orderbook_buy')
Sresult = selling[0].find_all(['tr'])
data = {}
c = 0
for item in Sresult:
    # You need to sum price and value? this sums up price and value
    tds = item.find_all(["td"])[2:]
    c += 1
    summa = 0
    for elem in tds:
        elemVal = float(elem.text.replace(" CAD", ""))
        summa += elemVal
        data[c] = summa
print data

这给了我们:

 {2: 200.16001, 3: 544.5600000000001, 4: 543.072, 5: 6969.35103, 6: 1000.14005, 7: 108.601, 8: 472.95, 9: 533.26, 10: 180.0, 11: 271.34000000000003, 12: 178.0, 13: 242.94, 14: 334.85, 15: 176.0, 16: 320.08000000000004, 17: 1269.05023, 18: 1071.33022, 19: 2618.9202, 20: 97.12008999999999, 21: 656.48005}

数字 2 是 200.16001,等于 90.76001 + 109.40,依此类推...

于 2013-07-19T18:53:15.473 回答