使用Python请求的“json.decoder.JSONDecodeError:期望值”

2024-04-27 20:38:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我得到一个错误:

raise JSONDecodeError("Expecting value", s, err.value) from None json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

代码如下:

import requests
import json
search_terms = input('Name of university: ')
search_year_start = input('Search start year (YYYY): ')
search_end_start = input('Search end year (YYYY): ')
    
parameters = {"affiliation": search_terms}
response = requests.get("https://api.ror.org/organizations", params=parameters)    
ror_response = response.json()
    
name_variations = []
for organisation in ror_response['items']:
    if organisation['chosen']==True:
        name_variations.append(organisation['organization']['name'])
        name_variations.extend(organisation['organization']['aliases'])
        for relationship in organisation['organization']['relationships']:
            if relationship['type'] == 'Child':
               name_variations.append(relationship['label'])
    
seed_dois=[]
for variation in name_variations:
    response = requests.get(url=f"https://api.crossref.org/works?filter=from-pub-date:{search_year_start},until-pub-date:{search_end_start},has-affiliation:true&query.affiliation={variation}&sample=1&select=DOI")
    response_data =response.json()
    seed_dois.append(response_data['message']['items'])
    
for doi in seed_dois:
    response=requests.get(url=f"https://opencitations.net/index/coci/api/v1/citations/{doi}")
    response_data = response.json()
    
citing_dois=[]
for doi in response_data:
        cited_by=doi['citing']
        citing_dois.append(cited_by)

据我所知,这个错误与seed_dois[]有关,当我试图把它放到requests.get中时。如果我对URL进行硬编码,那么在这一点之后一切都会正常工作,而在这一点之前一切都会正常工作

为什么我会出现这个错误,我该如何解决它

编辑

完全回溯

  File "c:\Users\timc0\Desktop\py4e\patent_app\ror.py", line 43, in <module>
    response_data = response.json()
  File "C:\Users\timc0\AppData\Local\Programs\Python\Python39\lib\site-packages\requests\models.py", line 910, in json
    return complexjson.loads(self.text, **kwargs)
  File "C:\Users\timc0\AppData\Local\Programs\Python\Python39\lib\json\__init__.py", line 346, in loads
    return _default_decoder.decode(s)
  File "C:\Users\timc0\AppData\Local\Programs\Python\Python39\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Users\timc0\AppData\Local\Programs\Python\Python39\lib\json\decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)````

Tags: nameinjsonforsearchvalueresponseline