Skip to content

Instantly share code, notes, and snippets.

@dridk
Last active March 6, 2021 09:22
Show Gist options
  • Save dridk/07770677340f1dd68960819a2657c50b to your computer and use it in GitHub Desktop.
Save dridk/07770677340f1dd68960819a2657c50b to your computer and use it in GitHub Desktop.
How to download, from wikidata, all picture with a penis
from qwikidata.sparql import return_sparql_query_results
import urllib.request
from urllib.parse import urlparse, unquote
import os
q = """
SELECT ?item ?object ?image
WHERE
{
?item wdt:P31/wdt:P279* wd:Q4502142 .
?item wdt:P180 ?object .
?object wdt:P279* wd:Q58 .
?item wdt:P18 ?image .
}
"""
items = return_sparql_query_results(q)
for item in items["results"]["bindings"]:
img_url = item["image"]["value"]
filename = os.path.basename(urlparse(unquote(img_url)).path)
print("Download", filename)
urllib.request.urlretrieve(img_url, filename)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment