我试图使用Python pandas从poloniex API中提取货币对。
我相信返回的数据只是一个列名:
Columns: [{"BTC_BCN":{"BTC":"479.74697466", "BCN":"1087153595.32266165"}, "BTC_BELA":{"BTC":"32.92293515", "BELA":"1807337.13247948"}, "BTC_BLK":{"BTC":"25.70374054", "BLK":"606717.86348734"}, "BTC_BTCD":{"BTC":"24.32220571", "BTCD":"1264.02352237"}, "BTC_BTM":{"BTC":"11.57816905", "BTM":"80673.47934437"}, "BTC_BTS":{"BTC":"1102.88787610", "BTS":"30426626.64558044"}
我想要的结果:BTC_BCN, BTC_BELA, BTC_BLK,
等......
但是不确定是否有一种简单的方法可以在没有字符串解析的情况下获得它,因为它们看起来只是列名。
码:
from bs4 import BeautifulSoup
import csv
import urllib2
import pandas as pd
try:
from StringIO import StringIO
except:
from io import StringIO
sock= urllib2.urlopen('https://poloniex.com/public?command=return24hVolume')
link=sock.read()
soup = BeautifulSoup(link,'lxml')
csv_data = StringIO(soup.text)
df=pd.read_csv(csv_data,delimiter=' *, *',engine='python')
df2=df.iloc[1:2,0:20]
你根本不需要BeautifulSoup
。网页的内容是JSON - 直接用.read_json()
解析它:
df = pd.read_json('https://poloniex.com/public?command=return24hVolume')