通过 pandas 访问远程URL时处理HTTP身份验证 [英] Handling HTTP authentication when accesing remote urls via pandas

查看:42
本文介绍了通过 pandas 访问远程URL时处理HTTP身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Pandas具有非常方便的功能,可以从URL读取csv和其他格式. 但是,当数据通过简单的http身份验证进行保护时,Pandas无法提示用户输入身份验证详细信息(用户ID,密码).解决此限制的最佳方法是什么?

Pandas has a very convenient ability to read csv and other formats from urls. However,when the data is protected by simple http authentication, Pandas is not capable to prompt the user for the the authentication details (userid, password). What is the best way to fix this limitation?

我当前正在做的是:

response = requests.get('http://my.data.url/metrics/crawler/counts', auth=HTTPBasicAuth('userid', 'password'), stream=True)
pd.read_csv(response.raw)

还有更好的方法吗?

推荐答案

无法测试,但是看起来基本的访问身份验证可以在url中进行编码.来自维基百科:

Not able to test, but it looks like basic access authentication can be encoded within the url. From wikipedia:

URL编码

在访问基本访问身份验证时,客户端可以通过在URL中的主机名前添加username:password @来避免登录提示.例如,以下内容将使用安全的HTTPS协议访问网站www.example.com上的页面index.html,并通过基本授权提供用户名Aladdin和密码OpenSesame凭据: https://Aladdin:OpenSesame@www.example.com/index.html

A client may avoid a login prompt when accessing a basic access authentication by prepending username:password@ to the hostname in the url. For example, the following would access the page index.html at the web site www.example.com with the secure HTTPS protocol and provide the username Aladdin and the password OpenSesame credentials via basic authorization: https://Aladdin:OpenSesame@www.example.com/index.html

希望有帮助!

这篇关于通过 pandas 访问远程URL时处理HTTP身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆