从python的urllib2访问netflix api会导致500错误

时间:2015-02-12 03:54:17

标签: python json urllib2 netflix

我目前正在尝试修复名为NetfliXBMC的Kodi插件。

它使用此网址获取有关特定电影的信息:

http://www.netflix.com/JSON/BOB?movieid=<SOMEID>

在尝试构建一个最小的案例来问这个问题时,我发现甚至没有必要登录来访问信息,这简化了我的问题。

查询有关电影的信息可以从wget,curl,隐身Chrome等工作。它从来没有在urllib2上运行:

# wget works just fine
$: wget -q -O- http://www.netflix.com/JSON/BOB?movieid=80021955
    {"contextData":"{\"cookieDisclosure\":{\"data\":{\"showCookieBanner\":false}}}","result":"success","actionErrors":null,"fieldErrors":null,"actionMessages":null,"data":[output omitted for brevity]}

# so does curl
$: curl http://www.netflix.com/JSON/BOB?movieid=80021955
        {"contextData":"{\"cookieDisclosure\":{\"data\":{\"showCookieBanner\":false}}}","result":"success","actionErrors":null,"fieldErrors":null,"actionMessages":null,"data":[output omitted for brevity}


# but python's urllib always gets a 500
$: python -c "import urllib2; urllib2.urlopen('http://www.netflix.com/JSON/BOB?movieid=80021955').read()"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/lib/python2.7/urllib2.py", line 127, in urlopen
    return _opener.open(url, data, timeout)
  File "/usr/lib/python2.7/urllib2.py", line 410, in open
    response = meth(req, response)
  File "/usr/lib/python2.7/urllib2.py", line 523, in http_response
    'http', request, response, code, msg, hdrs)
  File "/usr/lib/python2.7/urllib2.py", line 448, in error
    return self._call_chain(*args)
  File "/usr/lib/python2.7/urllib2.py", line 382, in _call_chain
    result = func(*args)
  File "/usr/lib/python2.7/urllib2.py", line 531, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 500: Internal Server Error

$: python --version
Python 2.7.6

到目前为止我尝试过:几个不同的用户代理字符串,使用cookie jar初始化urlopener,普通的旧urllib(不会引发异常,但会收到相同的错误页面)。

我真的很好奇为什么会这样。提前谢谢!

1 个答案:

答案 0 :(得分:1)

原来这是netflix上的一个错误&#39;没有发送Accept标题的一方。

这不起作用:

opener = urllib2.build_opener()
opener.open("http://www.netflix.com/JSON/BOB?movieid=80021955")

添加正确的接受标头使其有效:

opener = urllib2.build_opener()
mimeAccept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
opener.addheaders = [('Accept', mimeAccept)]
opener.open("http://www.netflix.com/JSON/BOB?movieid=80021955")
[...]

当然,还有另一个错误:当请求中明显出现问题时,它会返回500 internal server error而不是400 bad request