Python 使用请求从URL获取网站数据

Python 使用请求从URL获取网站数据,python,json,python-requests,Python,Json,Python Requests,尝试使用请求检索网站数据并将其转换为json文件。虽然它适用于一个url,但不适用于其他类型的url。我肯定这只是一个糟糕的url,但我认为我的语法也不正确 r = requests.get('https://icanhazdadjoke.com/', headers={'Accept': 'application/json'}) data = r.json() data output: {'id': 'Z8UDIRuXLmb', 'joke': 'Who did the wizard ma

尝试使用请求检索网站数据并将其转换为json文件。虽然它适用于一个url,但不适用于其他类型的url。我肯定这只是一个糟糕的url,但我认为我的语法也不正确

r = requests.get('https://icanhazdadjoke.com/', headers={'Accept': 'application/json'})
data = r.json()
data

output: {'id': 'Z8UDIRuXLmb',
 'joke': 'Who did the wizard marry? His ghoul-friend',
 'status': 200}

但当我尝试时:

r = requests.get('https://icanhazdadjoke.com/', headers={'Accept': 'application/json'})
data = r.json()
print(data)

or

data = requests.get('http://web.archive.org/web/20180326124748/https://www.theguardian.com/side-hustle', headers={'Accept': 'application/json'}).json()
print(data)

我得到了这个完整的回溯错误:

---------------------------------------------------------------------------
JSONDecodeError                           Traceback (most recent call last)
<ipython-input-50-92f4c7755f72> in <module>()
----> 1 data = requests.get('http://web.archive.org/web/20180326124748/https://www.theguardian.com/side-hustle', headers={'Accept': 'application/json'}).json()
      2 data

3 frames
/usr/local/lib/python3.6/dist-packages/requests/models.py in json(self, **kwargs)
    896                     # used.
    897                     pass
--> 898         return complexjson.loads(self.text, **kwargs)
    899 
    900     @property

/usr/lib/python3.6/json/__init__.py in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
    352             parse_int is None and parse_float is None and
    353             parse_constant is None and object_pairs_hook is None and not kw):
--> 354         return _default_decoder.decode(s)
    355     if cls is None:
    356         cls = JSONDecoder

/usr/lib/python3.6/json/decoder.py in decode(self, s, _w)
    337 
    338         """
--> 339         obj, end = self.raw_decode(s, idx=_w(s, 0).end())
    340         end = _w(s, end).end()
    341         if end != len(s):

/usr/lib/python3.6/json/decoder.py in raw_decode(self, s, idx)
    355             obj, end = self.scan_once(s, idx)
    356         except StopIteration as err:
--> 357             raise JSONDecodeError("Expecting value", s, err.value) from None
    358         return obj, end

JSONDecodeError: Expecting value: line 2 column 1 (char 1)

---------------------------------------------------------------------------
JSONDecodeError回溯(最近一次调用)
在()
---->1数据=请求。获取('http://web.archive.org/web/20180326124748/https://www.theguardian.com/side-hustle',headers={'Accept':'application/json'}).json()
2数据
3帧
/json中的usr/local/lib/python3.6/dist-packages/requests/models.py(self,**kwargs)
896已使用。
897通行证
-->898返回complexjson.load(self.text,**kwargs)
899
900@property
/加载中的usr/lib/python3.6/json/\uuuuu init\uuuuuuu.py(s、编码、cls、对象钩子、解析浮点数、解析整型、解析常数、对象对钩子,**kw)
352 parse_int为无,parse_float为无,且
353 parse_常量为None且对象_pairs_hook为None且非kw):
-->354返回\u默认\u解码器。解码
355如果cls为无:
356 cls=JSONDecoder
/解码中的usr/lib/python3.6/json/decoder.py(self,s,_w)
337
338         """
-->339 obj,end=self.raw\u decode(s,idx=\u w(s,0.end())
340 end=_w(s,end).end()
341如果结束!=长度:
/原始解码中的usr/lib/python3.6/json/decoder.py(self、s、idx)
355 obj,end=自扫描一次(s,idx)
356除了停止迭代作为错误:
-->357将JSONDecodeError(“预期值”,s,err.value)从None提升
358返回obj,结束
JSONDecodeError:应为值:第2行第1列(字符1)

这只是一个错误的url吗?谢谢!

问题不在Python代码中

您可以使用Linux中的wget cli程序检查这一点

您可以获得该网页的HTML代码,但是

wget --header="Accept:application/json" https://icanhazdadjoke.com
您得到的JSON数据包含一个类似于您所展示的笑话

但是,对于出现错误的网站,即使使用
--header=“Accept:application/JSON”
,也会得到HTML代码而不是JSON代码。因此,尝试将返回的数据解码为JSON将产生错误,因为它不是JSON

wget --header="Accept:application/json" https://icanhazdadjoke.com