我在獲取 api 資料時遇到了一些問題,資料將以 JQuery 格式出現,如下所示
當我使用 responce.text 時,資料以以下 belo 格式通過 API
client_identified":false,"client_pep_answered":false,"title":"Pablo '畢加索","body":"之后。“弗雷德斯馬彭”。Signerade Picasso i trycket samt ' 'numrerade i blyerts 260\/500。巴黎和平運動版。Sju ' 'litografier, 28 x 38 cm (vardera ' 'blad).","petits":"1974.","condition":"Samtliga ramade. Ej 檢查你的''ramar。N\u00e5got gulnade。N\u00e5got varierande ramm\u00e5tt:''36 x 47 cm och 36 x 46''cm.","provenance":null,"exhibition":null,"literature":null,"change":null,"context ":null,"artist_name":"Pablo ' 'Picasso","artist_years":"Spanien\/Frankrike 1881-1973。''
我想以json格式輸出
img_alt":"Pablo ' ' Picasso Spanien\/Frankrike 1881-1973。后來。“弗雷德斯馬彭”。Signerade ' 'Picasso i trycket samt numrerade i blyerts 260\/500. 巴黎和平運動版。Sju litografier,28 x 38 厘米(vardera ''blad)。","images":[{"id":"3512497","catalog_nr":"592","path210":"\/view\/210\ /3512497.png?1638276330","path464":"\/view\/464\/3512497.jpg?1638276330","path900":"\/view\/900\/3512497.jpg?1638276330","大":"\/view\/large\/3512497.jpg?1638276330"},{"id":"3512497-1","catalog_nr":"592-1","path210":"\/view\/ 210\/3512497-1.png?1638276323","path464":"\/view\/464\/3512497-1。
我的代碼在使用 json 時出現錯誤
import requests
import pymongo
import json
from pprint import pprint
url = "https://live.uppsalaauktion.se/api/searcharchive/execute?callback=jQuery22401117076961935719_1646032781147&estimate_max=100000000&estimate_min=1000&hammer_max=100000000&hammer_min=1000&query=picasso&per_page=100&language_id=2&paging=1&token=&_=1646032781148"
payload = "callback=jQuery22401117076961935719_1646032781147&estimate_max=100000000&estimate_min=1000&hammer_max=100000000&hammer_min=1000&query=picasso&per_page=100&language_id=2&paging=1&token=&_=1646032781148"
headers = {
'Content-Type': 'text/json',
'Cookie': 'session=d1c582b3ef346ceedf3645751cb8c452eb0b5d0a~621c77ed469a41-62319936'
}
response = requests.request("GET", url, headers=headers, data=payload)
jsonData=response.json()
pprint(jsonData)
錯誤:
Input In [25], in <module>
6 headers = {
7 # 'Content-Type': 'text/plain',
8 'Content-Type': 'text/json',
9
10 'Cookie': 'session=d1c582b3ef346ceedf3645751cb8c452eb0b5d0a%7E621c77ed469a41-62319936'
11 }
13 response = requests.request("GET", url, headers=headers, data=payload)
---> 14 jsonData=response.json()
15 pprint(jsonData)
File /usr/lib/python3/dist-packages/requests/models.py:888, in Response.json(self, **kwargs)
886 if encoding is not None:
887 try:
--> 888 return complexjson.loads(
889 self.content.decode(encoding), **kwargs
890 )
891 except UnicodeDecodeError:
892 # Wrong UTF codec detected; usually because it's not UTF-8
893 # but some other 8-bit codec. This is an RFC violation,
894 # and the server didn't bother to tell us what codec *was*
895 # used.
896 pass
File /usr/lib/python3/dist-packages/simplejson/__init__.py:518, in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, use_decimal, **kw)
467 """Deserialize ``s`` (a ``str`` or ``unicode`` instance containing a JSON
468 document) to a Python object.
469
(...)
512
513 """
514 if (cls is None and encoding is None and object_hook is None and
515 parse_int is None and parse_float is None and
516 parse_constant is None and object_pairs_hook is None
517 and not use_decimal and not kw):
--> 518 return _default_decoder.decode(s)
519 if cls is None:
520 cls = JSONDecoder
File /usr/lib/python3/dist-packages/simplejson/decoder.py:370, in JSONDecoder.decode(self, s, _w, _PY3)
368 if _PY3 and isinstance(s, bytes):
369 s = str(s, self.encoding)
--> 370 obj, end = self.raw_decode(s)
371 end = _w(s, end).end()
372 if end != len(s):
File /usr/lib/python3/dist-packages/simplejson/decoder.py:400, in JSONDecoder.raw_decode(self, s, idx, _w, _PY3)
398 elif ord0 == 0xef and s[idx:idx 3] == '\xef\xbb\xbf':
399 idx = 3
--> 400 return self.scan_once(s, idx=_w(s, idx).end())
JSONDecodeError: Expecting value: line 1 column 1 (char 0)
如何獲取/轉換 json 格式的資料。請幫助
uj5u.com熱心網友回復:
import requests
import json
url = "https://live.uppsalaauktion.se/api/searcharchive/execute?callback=jQuery22401117076961935719_1646032781147&estimate_max=100000000&estimate_min=1000&hammer_max=100000000&hammer_min=1000&query=picasso&per_page=100&language_id=2&paging=1&token=&_=1646032781148"
payload = "callback=jQuery22401117076961935719_1646032781147&estimate_max=100000000&estimate_min=1000&hammer_max=100000000&hammer_min=1000&query=picasso&per_page=100&language_id=2&paging=1&token=&_=1646032781148"
headers = {'Content-Type': 'text/json','Cookie':'session=d1c582b3ef346ceedf3645751cb8c452eb0b5d0a~621c77ed469a41-62319936'}
response = requests.request("GET", url, headers=headers, data=payload)
# convert response to str
content = response.text
# get part of str
content_part = content.split(',"objects":')[1].split(',"pagination":[{"page":1,"selected":true},{"page":2,'
'"selected":false},{"page":3,"selected":false},{"page":4,'
'"selected":false},{"page":5,"selected":false}]')[0]
# convert to json
content_json = json.loads(content_part)
# print id
print(content_json[0]['id'])
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/435255.html
上一篇:選中選項時禁用某些輸入
