Created
November 15, 2020 20:33
-
-
Save thisismattmiller/42c9d5981ee233c6288194af234ac6e4 to your computer and use it in GitHub Desktop.
Code from this video: https://youtu.be/SzfC3KFqmPs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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