首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >BeautifulSoup双层

BeautifulSoup双层
EN

Stack Overflow用户
提问于 2013-07-20 02:20:20
回答 2查看 153关注 0票数 1

我正在构建一个小模块,它的任务是做以下事情:用-Read库打开一个网页(cavirtex.com/orderbook) -Get,然后用漂亮的-parse打开它并获取body.div(id='xx')现在,我被困在这里了。我想要重新美化结果,在更大的tr中迭代两行td,并获得值并求和。如果有人知道怎么做,请给我解释一下,因为我已经被困在这里好几个小时了。哦,这是我的源代码:

代码语言:javascript
运行
复制
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
EN

回答 2

Stack Overflow用户

发布于 2013-07-20 02:53:35

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

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

该链接包含友好的JSON中所需的所有信息。

示例:

代码语言:javascript
运行
复制
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

大多数比特币交易所都支持相同的应用编程接口,如bitcoincharts.com所述:http://bitcoincharts.com/about/exchanges/

享受吧!

票数 2
EN

Stack Overflow用户

发布于 2013-07-20 02:53:15

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

代码语言:javascript
运行
复制
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

这为我们提供了:

代码语言:javascript
运行
复制
 {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,以此类推...

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17752947

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档