Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用python的SPARQL请求可以工作,但一旦请求的时间范围被扩展,就会导致JSONDecodeError_Json_Python 3.x_Python Requests_Sparql_Wikidata - Fatal编程技术网

使用python的SPARQL请求可以工作,但一旦请求的时间范围被扩展,就会导致JSONDecodeError

使用python的SPARQL请求可以工作,但一旦请求的时间范围被扩展,就会导致JSONDecodeError,json,python-3.x,python-requests,sparql,wikidata,Json,Python 3.x,Python Requests,Sparql,Wikidata,我在试图用python从wikidata请求数据时遇到了一个问题,非常感谢您的帮助 在正常工作时(如代码段1所示),当查询稍微修改以覆盖更长的时间时,运行代码会导致JSONDecodeError错误(如代码段2所示) 代码片段1: query = """ SELECT ?person ?personLabel ?dob ?place_of_birth ?place_of_birthLabel ?date_of_death ?place_of_death ?place

我在试图用python从wikidata请求数据时遇到了一个问题,非常感谢您的帮助

在正常工作时(如代码段1所示),当查询稍微修改以覆盖更长的时间时,运行代码会导致JSONDecodeError错误(如代码段2所示)

代码片段1:

query = """
SELECT ?person ?personLabel ?dob ?place_of_birth ?place_of_birthLabel ?date_of_death ?place_of_death ?place_of_deathLabel ?political_party ?political_partyLabel ?sex_or_gender ?sex_or_genderLabel ?Wikimedia_import_URL ?occupation ?occupationLabel ?work_location ?work_locationLabel ?educated_at ?educated_atLabel ?imported_from_Wikimedia_project ?imported_from_Wikimedia_projectLabel ?source_website_for_the_property ?stated_in ?stated_inLabel ?religion ?religionLabel ?VIAF_ID ?ISNI ?Deutsche_Biographie_ID ?DBS_ID ?place_of_detention ?place_of_detentionLabel ?country_of_citizenship ?country_of_citizenshipLabel ?member_of_military_unit ?member_of_military_unitLabel ?conflict ?conflictLabel ?military_rank ?military_rankLabel ?military_branch ?military_branchLabel ?participant_in ?participant_inLabel ?award_received ?award_receivedLabel ?described_by_source ?described_by_sourceLabel ?academic_degree ?academic_degreeLabel ?field_of_work ?field_of_workLabel ?noble_title ?noble_titleLabel WHERE {
  ?person wdt:P31 wd:Q5;
    wdt:P569 ?dob.
  FILTER(("1870-01-02"^^xsd:dateTime <= ?dob) && (?dob <= "1870-01-04"^^xsd:dateTime))
  SERVICE wikibase:label { bd:serviceParam wikibase:language "de". }
  OPTIONAL { ?person wdt:P19 ?place_of_birth. }
  OPTIONAL { ?person wdt:P570 ?date_of_death. }
  OPTIONAL { ?person wdt:P20 ?place_of_death. }
  OPTIONAL { ?person wdt:P102 ?political_party. }
  OPTIONAL { ?person wdt:P21 ?sex_or_gender. }
  OPTIONAL { ?person wdt:P4656 ?Wikimedia_import_URL. }
  OPTIONAL { ?person wdt:P106 ?occupation. }
  OPTIONAL { ?person wdt:P937 ?work_location. }
  OPTIONAL { ?person wdt:P69 ?educated_at. }
  OPTIONAL { ?person wdt:P143 ?imported_from_Wikimedia_project. }
  OPTIONAL { ?person wdt:P1896 ?source_website_for_the_property. }
  OPTIONAL { ?person wdt:P248 ?stated_in. }
  OPTIONAL { ?person wdt:P140 ?religion. }
  OPTIONAL { ?person wdt:P214 ?VIAF_ID. }
  OPTIONAL { ?person wdt:P213 ?ISNI. }
  OPTIONAL { ?person wdt:P7902 ?Deutsche_Biographie_ID. }
  OPTIONAL { ?person wdt:P4007 ?DBS_ID. }
  OPTIONAL { ?person wdt:P5019 ?occupation. }
  OPTIONAL { ?person wdt:P2632 ?place_of_detention. }
  OPTIONAL { ?person wdt:P27 ?country_of_citizenship. }
  OPTIONAL { ?person wdt:P7779 ?member_of_military_unit. }
  OPTIONAL { ?person wdt:P607 ?conflict. }
  OPTIONAL { ?person wdt:P410 ?military_rank. }
  OPTIONAL { ?person wdt:P241 ?military_branch. }
  OPTIONAL { ?person wdt:P1344 ?participant_in. }
  OPTIONAL { ?person wdt:P166 ?award_received. }
  OPTIONAL { ?person wdt:P1343 ?described_by_source. }
  OPTIONAL { ?person wdt:P512 ?academic_degree. }
  OPTIONAL { ?person wdt:P101 ?field_of_work. }
  OPTIONAL { ?person wdt:P97 ?noble_title. }
}
"""

import requests

url = 'https://query.wikidata.org/sparql'
r = requests.get(url, params={'format': 'json', 'query': query})

json_format = r.json()
FILTER(("1870-01-02"^^xsd:dateTime <= ?dob) && (?dob <= "1870-01-04"^^xsd:dateTime))
在浏览internet寻求帮助后,我在代码行中添加了“strict=False”,该行负责解码(以消除因检索到的数据中包含“\n”等模式而导致的问题),从而生成代码片段3:

import requests

url = 'https://query.wikidata.org/sparql'
r = requests.get(url, params={'format': 'json', 'query': query})

json_format = r.json(strict=False)
然而,这触发了另一种解码错误:

json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 2031964 column 9 (char 56246827)
json.decoder.JSONDecodeError: Expecting ':' delimiter: line 1521988 column 12 (char 42127071)
因此,我再次修改了代码,试图从请求库中避免.json()函数(代码片段4):

导致第三种解码错误:

json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 2031964 column 9 (char 56246827)
json.decoder.JSONDecodeError: Expecting ':' delimiter: line 1521988 column 12 (char 42127071)
在设置“strict=True”时,我还尝试手动将上一个代码段中定义的变量json_str中的模式替换为“\n”。不幸的是,我的方法都不管用。 好了,现在就到此为止。如果有人对如何解决这个问题有想法,我将不胜感激:)

干杯,
Marius

要么流因为明显较大的结果而损坏,要么Blazegraoh的JSON序列化有一些bug。你检查过命令行的结果了吗?我用
curl
从命令行尝试了你的第二次查询,正如预期的那样,它1)导致超时2)这就是JSON不完整的原因,3)包含错误stacktrace,4)使其无效JSON,5)出于充分的理由无法使用标准JSON解析器进行解析。长话短说,您的查询对于公共共享端点来说太复杂了。谢谢您的评论!根据您的评估,我解决了这个问题,每月和每年都通过一个单独的查询进行循环,以降低查询的复杂性。很好用!
json.decoder.JSONDecodeError: Expecting ':' delimiter: line 1521988 column 12 (char 42127071)