Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Code from this video: https://youtu.be/SzfC3KFqmPs
import requests
import json
url = "https://www.wikidata.org/wiki/Special:EntityData/"
qnumbers = ['Q43260736', 'Q51827254', 'Q56471556']
all_properties = {}
for qnum in qnumbers:
useurl = url + qnum + '.json'
headers = {
'Accept' : 'application/json',
'User-Agent': 'USER thisismattmiller - Test Script '
}
r = requests.get(useurl, headers=headers)
data = json.loads(r.text)
# print(data)
# print(data['entities'][qnum]['claims'])
properties = list(data['entities'][qnum]['claims'].keys())
print(properties)
for p in properties:
if p not in all_properties:
all_properties[p] = 0
all_properties[p]+=1
print(all_properties)
import requests
import json
url = "https://query.wikidata.org/sparql"
sparql = """
#Cats
SELECT ?item ?itemLabel ?occupation ?occupationLabel
WHERE
{
?item wdt:P31 wd:Q146.
optional{
?item wdt:P106 ?occupation
}
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
"""
params = {
'query' : sparql
}
headers = {
'Accept' : 'application/json',
'User-Agent': 'USER thisismattmiller - Test Script '
}
r = requests.get(url, params=params, headers=headers)
data = json.loads(r.text)
# "results" : {
# "bindings" : [ {
# "item" : {
occupationTotals = {}
for result in data['results']['bindings']:
# print(json.dumps(result,indent=2))
if 'occupationLabel' in result:
print(result['occupationLabel']['value'])
if result['occupationLabel']['value'] not in occupationTotals:
occupationTotals[result['occupationLabel']['value']] = 0
occupationTotals[result['occupationLabel']['value']]+=1
print(occupationTotals)
import requests
import json
url = "https://query.wikidata.org/sparql"
sparql = """
SELECT ?item ?itemLabel ?occupation ?occupationLabel
WHERE
{
VALUES ?item {<REPLACEME>}
optional{
?item wdt:P106 ?occupation
}
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
"""
qnumbers = ['Q43260736', 'Q51827254', 'Q56471556']
qnumbers_with_prefix = []
for q in qnumbers:
qnumbers_with_prefix.append(f"wd:{q}")
replace_string = " ".join(qnumbers_with_prefix)
sparql = sparql.replace('<REPLACEME>',replace_string)
params = {
'query' : sparql
}
headers = {
'Accept' : 'application/json',
'User-Agent': 'USER thisismattmiller - Test Script '
}
r = requests.get(url, params=params, headers=headers)
data = json.loads(r.text)
occupationTotals = {}
for result in data['results']['bindings']:
# print(json.dumps(result,indent=2))
if 'occupationLabel' in result:
print(result['occupationLabel']['value'])
if result['occupationLabel']['value'] not in occupationTotals:
occupationTotals[result['occupationLabel']['value']] = 0
occupationTotals[result['occupationLabel']['value']]+=1
print(occupationTotals)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment