【问题标题】:i have been trying to parse a website and when using urllib2.urlopen i have been getting a error我一直在尝试解析网站,在使用 urllib2.urlopen 时出现错误
【发布时间】:2017-04-23 09:02:55
【问题描述】:

谁能解释我通过python登录这个链接(ftpservice.acesphere.com)

【问题讨论】:

  • 在您的请求中带上 cookie/authen 令牌;否则,您的请求将是匿名的。

标签: python-2.7 python-requests urllib2 urllib urlopen


【解决方案1】:

您尝试访问的 URL 需要 NTLM 身份验证。你可以试试python-ntlm包:

from ntlm import HTTPNtlmAuthHandler
import urllib2

url = "http://ftpservice.acesphere.com/stocks/indices/master/indicesmaster_new.ace"
user = r'domain\user'
password = "password"

pm = urllib2.HTTPPasswordMgrWithDefaultRealm()
pm.add_password(None, "http://ftpservice.acesphere.com/", user, password)
auth = HTTPNtlmAuthHandler.HTTPNtlmAuthHandler(pm)
opener = urllib2.build_opener(auth)
urllib2.install_opener(opener)

response = urllib2.urlopen(url)
print response.read()

【讨论】:

  • 我尝试使用我的密码和用户凭据输入.. 但我最终得到了同样的错误
  • 您可以使用空凭据或假凭据显示您的实际代码。
  • 我要解析的网站就像 .ace 不像一个普通的网站,它的结尾是 .html。这是我错误的原因
  • 没有。你试过我的代码吗?你有同样的错误吗?
  • 是的,在你说之前我就明白了......那是 NTLM 身份验证......但无论如何感谢您的时间和您分享知识的热情
【解决方案2】:

你得到这个异常:

urllib2.HTTPError: HTTP Error 401: Unauthorized

这意味着该网站正在返回 HTTP 401 Unauthorized 状态代码。捕获异常或修改您的请求以不产生此错误。

另请参阅:urllib2 documentation

【讨论】:

  • 你能详细解释一下吗
  • @SandeepreddyBhimireddy 你想解释什么?
猜你喜欢
  • 2022-01-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-12
  • 2022-01-04
  • 1970-01-01
  • 2018-02-02
  • 1970-01-01
相关资源
最近更新 更多