!pip install wikitables
!pip install sparqlwrapper
!pip install mwparserfromhell
!pip install 
Requirement already satisfied: wikitables in /srv/paws/lib/python3.6/site-packages
Requirement already satisfied: mwparserfromhell>=0.4.3 in /srv/paws/lib/python3.6/site-packages (from wikitables)
Requirement already satisfied: requests>=2.9.1 in /srv/paws/lib/python3.6/site-packages (from wikitables)
Requirement already satisfied: chardet<3.1.0,>=3.0.2 in /srv/paws/lib/python3.6/site-packages (from requests>=2.9.1->wikitables)
Requirement already satisfied: idna<2.8,>=2.5 in /srv/paws/lib/python3.6/site-packages (from requests>=2.9.1->wikitables)
Requirement already satisfied: urllib3<1.24,>=1.21.1 in /srv/paws/lib/python3.6/site-packages (from requests>=2.9.1->wikitables)
Requirement already satisfied: certifi>=2017.4.17 in /srv/paws/lib/python3.6/site-packages (from requests>=2.9.1->wikitables)
Requirement already satisfied: sparqlwrapper in /srv/paws/lib/python3.6/site-packages
Requirement already satisfied: rdflib>=4.0 in /srv/paws/lib/python3.6/site-packages (from sparqlwrapper)
Requirement already satisfied: pyparsing in /srv/paws/lib/python3.6/site-packages (from rdflib>=4.0->sparqlwrapper)
Requirement already satisfied: isodate in /srv/paws/lib/python3.6/site-packages (from rdflib>=4.0->sparqlwrapper)
Requirement already satisfied: six in /srv/paws/lib/python3.6/site-packages (from isodate->rdflib>=4.0->sparqlwrapper)
Requirement already satisfied: mwparserfromhell in /srv/paws/lib/python3.6/site-packages
import pywikibot

site = pywikibot.Site("de")
page = pywikibot.Page(site, u"Liste_der_Landkreise_in_Deutschland")
text = page.text
import re

from wikitables import import_tables
import mwparserfromhell

def get_page_title(raw_text):
    wikicode = mwparserfromhell.parse(raw_text)
    links = wikicode.filter_wikilinks()
    if links:
        return links[0].title

tables = import_tables('Liste_der_Landkreise_in_Deutschland', 'de')

def get_item_for_name(name):
    page = pywikibot.Page(site, name)
    return page.data_item()

wikipedia_ids = []
for row in tables[0].rows:
    page_name = get_page_title(str(row['Landkreis/Kreis'].raw))    
    item = get_item_for_name(page_name)
    print(page_name, item)
    wikipedia_ids.append(item.id)
from SPARQLWrapper import SPARQLWrapper, JSON

endpoint_url = "https://query.wikidata.org/sparql"

query = """
SELECT DISTINCT ?landkreis ?landkreisLabel ?coordinates WHERE {
  ?landkreis p:P31 ?statement. 
  ?statement ps:P31/wdt:P279* wd:Q106658.
  ?statement a wikibase:BestRank .
  MINUS{?statement pq:P582 ?end_time} .
  MINUS{?landkreis wdt:P576 ?dissolved. }
  OPTIONAL { ?landkreis wdt:P625 ?coordinates. }
  SERVICE wikibase:label { bd:serviceParam wikibase:language "de, en". }   
  
}"""


def get_results(endpoint_url, query):
    sparql = SPARQLWrapper(endpoint_url)
    sparql.setQuery(query)
    sparql.setReturnFormat(JSON)
    return sparql.query().convert()

def extract_id(url):
    [*rest, q_id] = url.split('/')
    return q_id

results = get_results(endpoint_url, query)

wikidata_ids = [result['landkreis']['value'] for result in results["results"]["bindings"]]
print("Landkreise ohne Auflösungsdatum (P576), die nicht in der Wikipedia Liste der Landkreise auftauchen.")
print("Hier muss recherchiert und das entsprechende P576 gesetzt werden.")
for item in wikidata_ids:
    if extract_id(item) not in wikipedia_ids:
        print(item)
Landkreise ohne Auflösungsdatum (P576), die nicht in der Wikipedia Liste der Landkreise auftauchen.
Hier muss recherchiert und das entsprechende P576 gesetzt werden.
http://www.wikidata.org/entity/Q1421347
http://www.wikidata.org/entity/Q1787568
http://www.wikidata.org/entity/Q1320758
http://www.wikidata.org/entity/Q1483352
http://www.wikidata.org/entity/Q1621433
http://www.wikidata.org/entity/Q1366141
http://www.wikidata.org/entity/Q1664105
http://www.wikidata.org/entity/Q1563834
http://www.wikidata.org/entity/Q1787540
http://www.wikidata.org/entity/Q1803071
http://www.wikidata.org/entity/Q1664009
http://www.wikidata.org/entity/Q1803073
http://www.wikidata.org/entity/Q1470516
http://www.wikidata.org/entity/Q1281905
http://www.wikidata.org/entity/Q1662276
http://www.wikidata.org/entity/Q18022911
http://www.wikidata.org/entity/Q49594
http://www.wikidata.org/entity/Q1803351
http://www.wikidata.org/entity/Q1803357