Skip to content

Instantly share code, notes, and snippets.

@lubianat
Created May 17, 2021 14:05
Show Gist options
  • Save lubianat/69d66dd008735726bc7ff21b6c62c684 to your computer and use it in GitHub Desktop.
Save lubianat/69d66dd008735726bc7ff21b6c62c684 to your computer and use it in GitHub Desktop.
import requests
import sys
import pandas as pd
url = sys.argv[1]
session = requests.Session() # so connections are recycled
resp = session.head(url, allow_redirects=True)
url_sparql = resp.url.replace("https://query.wikidata.org/#", "https://query.wikidata.org/sparql?query=")
r = requests.get(url_sparql, params = {'format':'json'})
df = pd.json_normalize(r.json()["results"]["bindings"])
df["qid"] = [url.split("/")[4] for url in df["work.value"]]
for i in df["qid"]:
print(i)
@lubianat
Copy link
Author

this assumes that there is variable being selected called "?work", by the way

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment