【问题标题】:Python 3.x get JSON from URLPython 3.x 从 URL 获取 JSON
【发布时间】:2013-09-15 06:59:06
【问题描述】:

各位程序员大家好,

今天我想使用 Python 3.3 从这个网站获取一些 JSON 数据:http://ladv.de/api/-apikey-redacted-/ausDetail?id=884&wettbewerbe=true&all=true

官方 API 告诉我调用这个 URL 会返回一些 JSON 数据。但是如果我使用下面的代码来获取它(我也在 stackoverflow 上找到了),它会抛出一个错误:

import urllib.request
import json

request = 'http://ladv.de/api/mmetzger/ausDetail?id=884&wettbewerbe=true&all=true'
response = urllib.request.urlopen(request)
obj = json.load(response)
str_response = response.readall().decode('utf-8')
obj = json.loads(str_response)
print(obj)

打印出来

Traceback (most recent call last):
  File "D:/ladvclient/testscrape.py", line 5, in <module>
    response = urllib.request.urlopen(request)
  File "C:\Python33\lib\urllib\request.py", line 156, in urlopen
    return opener.open(url, data, timeout)
  File "C:\Python33\lib\urllib\request.py", line 475, in open
    response = meth(req, response)
  File "C:\Python33\lib\urllib\request.py", line 587, in http_response
    'http', request, response, code, msg, hdrs)
  File "C:\Python33\lib\urllib\request.py", line 513, in error
    return self._call_chain(*args)
  File "C:\Python33\lib\urllib\request.py", line 447, in _call_chain
    result = func(*args)
  File "C:\Python33\lib\urllib\request.py", line 595, in http_error_default
        raise HTTPError(req.full_url, code, msg, hdrs, fp)
    urllib.error.HTTPError: HTTP Error 404: Not Found

错误在哪里,正确的代码是什么? 提前致谢,

论坛发帖人

【问题讨论】:

  • 请不要删除问题的内容:)。
  • 我认为 404: Not Found 说明了一切,但如果不是,请尝试在某些浏览器中打开链接。
  • 标题未涵盖问题,因为您获取特定站点并获得与使用 python 获取数据无关的未找到。使用 curl 会产生相同的未找到结果。

标签: python json url python-3.x get


【解决方案1】:

您尝试获取的网站不可用,如下所示:

http://ladv.de/api/-apikey-redacted-/ausDetail?id=884&wettbewerbe=true&all=true

您也可以自己阅读错误信息:

urllib.error.HTTPError: HTTP Error 404: Not Found

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-08
    • 2018-05-10
    • 1970-01-01
    • 2016-09-23
    • 1970-01-01
    • 2018-07-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多