Python错误:字节索引必须是整数或片,而不是str
我正试图从coinmarketcap.com上获取几枚硬币的价格数据。代码如下。最近我遇到了一个错误:字节索引必须是整数或片,而不是str。有人能帮我理解为什么会发生这种情况吗 有趣的是,当我只提取两三枚硬币的数据时,它工作得很好。但如果我想获取更多硬币的数据,它会开始给我这个错误消息Python错误:字节索引必须是整数或片,而不是str,python,python-3.x,runtime-error,Python,Python 3.x,Runtime Error,我正试图从coinmarketcap.com上获取几枚硬币的价格数据。代码如下。最近我遇到了一个错误:字节索引必须是整数或片,而不是str。有人能帮我理解为什么会发生这种情况吗 有趣的是,当我只提取两三枚硬币的数据时,它工作得很好。但如果我想获取更多硬币的数据,它会开始给我这个错误消息 import pandas as pd import http.client def get_close_px (coin): url = 'https://coinmarketcap.com/
import pandas as pd
import http.client
def get_close_px (coin):
url = 'https://coinmarketcap.com/currencies/'+str(coin)+'/historical-
data/?start=20180610&end=20180613'
try:
df = pd.read_html(url, parse_dates=[0])[0]
except (http.client.IncompleteRead) as e:
df = e.partial
df_close = df['Close**']
return df_close
coinArray = pd.concat([get_close_px ('Bitcoin'), get_close_px ('Ethereum'),
get_close_px ('Ripple'), get_close_px ('Bitcoin-Cash'),get_close_px
('Litecoin'), get_close_px ('Dash')],axis=1)
print (coinArray)
下面是完整的错误消息:
File "C:/Users/temp/.spyder-py3/temp - CoinmarketCap Webpage Scrap.py", line 16, in <module>
coinArray2 = pd.concat([get_close_px ('Litecoin'), get_close_px ('Dash'), get_close_px ('NEM')],axis=1)
File "C:/Users/temp/.spyder-py3/temp - CoinmarketCap Webpage Scrap.py", line 12, in get_close_px
df_close = df['Close**']
TypeError: byte indices must be integers or slices, not str
文件“C:/Users/temp/.spyder-py3/temp-CoinmarketCap网页Scrap.py”,第16行,在
coinArray2=pd.concat([get_close_px('Litecoin')、get_close_px('Dash')、get_close_px('NEM')),轴=1)
文件“C:/Users/temp/.spyder-py3/temp-CoinmarketCap网页Scrap.py”,第12行,在get\u close\u px中
df_close=df['close**]
TypeError:字节索引必须是整数或片,而不是str
谢谢你的帮助 请包含完整的错误消息。复制错误时遇到问题。你能提供你运行的导致错误的整个代码脚本吗?我已经包括了整个代码…这是一段很短的代码…我以前没有这个错误…最近刚刚发生的…我正在运行Anacoda 3.6.2hmmm,嗯
get\u close\u px('NEM'))
在您的错误消息中,但不在您的代码中…可能错误是由于没有完全运行代码片段中的内容造成的?我猜行为上的变化是try
构造现在执行部分,但由于某种原因,
部分除外,而e.partial
是不能用字符串索引的东西。