以身作则
this answer
,我修改了用于连接多行字符串的代码,如下所示:
import requests
my_variable = 'wd:Q1968435'
url = 'https://query.wikidata.org/sparql'
query = (
'SELECT ?item ?itemLabel ',
' WHERE ',
' {',
' ?item wdt:P31 '+my_variable+'.',
' SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }'
' }'
)
r = requests.get(url, params= {'format': 'json', 'query': "".join(query)})
data = r.json()
print("".join(query))
print(data)
上一个字符串串联的结果是:
SELECT ?item ?itemLabel WHERE { ?item wdt:P31 wd:Q1968435. SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }