检查HTTP状态(Python)

检查HTTP状态(Python),python,Python,有没有办法检查下面代码中的HTTP状态代码,因为我没有使用允许此操作的请求或urllib库 from pandas.io.excel import read_excel url = 'http://www.bankofengland.co.uk/statistics/Documents/yieldcurve/uknom05_mdaily.xls' #check the sheet number, spot: 9/9, short end 7/9 spot_curve = read_excel

有没有办法检查下面代码中的HTTP状态代码,因为我没有使用允许此操作的请求urllib

from pandas.io.excel import read_excel

url = 'http://www.bankofengland.co.uk/statistics/Documents/yieldcurve/uknom05_mdaily.xls'

#check the sheet number, spot: 9/9, short end 7/9
spot_curve = read_excel(url, sheetname=8) #Creates the dataframes
short_end_spot_curve = read_excel(url, sheetname=6)

# do some cleaning, keep NaN for now, as forward fill NaN is not recommended for yield curve
spot_curve.columns = spot_curve.loc['years:']
valid_index = spot_curve.index[4:]
spot_curve = spot_curve.loc[valid_index]
# remove all maturities within 5 years as those are duplicated in short-end file
col_mask = spot_curve.columns.values > 5
spot_curve = spot_curve.iloc[:, col_mask]
#Providing correct names
short_end_spot_curve.columns = short_end_spot_curve.loc['years:']
valid_index = short_end_spot_curve.index[4:]
short_end_spot_curve = short_end_spot_curve.loc[valid_index]

# merge these two, time index are identical
# ==============================================
combined_data = pd.concat([short_end_spot_curve, spot_curve], axis=1, join='outer')
# sort the maturity from short end to long end
combined_data.sort_index(axis=1, inplace=True)

def filter_func(group):
    return group.isnull().sum(axis=1) <= 50

combined_data = combined_data.groupby(level=0).filter(filter_func)
从pandas.io.excel导入读取excel
url='1〕http://www.bankofengland.co.uk/statistics/Documents/yieldcurve/uknom05_mdaily.xls'
#检查图纸编号,点:9/9,短端7/9
spot_curve=read_excel(url,sheetname=8)#创建数据帧
短\末端\点\曲线=读取\ excel(url,sheetname=6)
#进行一些清洁,暂时保留NaN,因为产量曲线不建议向前填充NaN
spot_curve.columns=spot_curve.loc['years:']
有效指数=点曲线。指数[4:]
spot_曲线=spot_曲线。loc[有效指数]
#删除5年内的所有到期日,因为这些到期日在短期文件中重复
col_mask=点_曲线.columns.values>5
spot_curve=spot_curve.iloc[:,col_mask]
#提供正确的姓名
short_end_spot_curve.columns=short_end_spot_curve.loc['years:']
有效指数=短终点点曲线。指数[4:]
短点曲线=短点曲线。loc[有效指数]
#合并这两个,时间索引是相同的
# ==============================================
组合数据=pd.concat([短端点点曲线,点曲线],轴=1,连接='outer')
#将到期日从短端排序到长端
组合数据。排序索引(轴=1,在位=真)
def过滤器功能(组):
返回组.isnull().sum(轴=1)在
pandas
中:
read\u excel
尝试使用
urllib2.urlopen
urllib.request.urlopen
而不是py3x)打开url并立即获取响应的
.read()
,而不存储http请求,如:

data = urlopen(url).read()
虽然您只需要部分excel,
pandas
每次都会下载整个excel。所以,我选了“jonnybazookatone”


最好将excel存储到本地,然后您可以先检查文件的状态代码和md5,以验证数据完整性或其他。

什么是
read_excel
?@LittleQ编辑了代码。Pandas使用urllib获取excel文件,据我所知,它似乎没有存储HTTP响应。你不能自己使用请求下载excel文件,然后在第一行中传递它吗?相关台词:@jonnybazookatone谢谢。使用
请求
是一种可行的方法,也是一种易于实现的更改。谢谢。我最终使用了
请求