import pywikibot
from pywikibot import pagegenerators as pg

site = pywikibot.Site("wikidata", "wikidata")
repo = site.data_repository()
property = "P462"
error_dict = {"Q13191": "Q39338",    #orange - "fruit": "color"
              "Q897": "Q208045",     #gold - "element": "color"
               "Q753": "Q2722041",   #copper - "element": "color"
              "Q25381": "Q679355",   #amber - "material": "color"
              "Q134862": "Q5069879", #champagne - "drink": "color"
              "Q1090": "Q317802",    #silver - "element": "color"
              "Q1173": "Q797446",    #burgundy - "region": "color
              "Q13411121": "Q5148721", #peach - "fruit": "color"
              }

def correct_claim(generator, key):
    for page in generator:
        item_dict = page.get()
        claim_list = item_dict["claims"][property]
        for claim in claim_list:
            trgt = claim.getTarget()
            if trgt.id == key:
                print("Correcting {} to {}".format(key, error_dict[key]))
                correct_page = pywikibot.ItemPage(repo, error_dict[key], 0)
                claim.changeTarget(correct_page)

for key in error_dict:
    wdq = "CLAIM[{}:{}]".format(property[1:], key[1:])
    generator = pg.WikidataQueryPageGenerator(wdq, site)
    generator = site.preloadpages(generator, pageprops=True)
    correct_claim(generator, key)
WARNING: Http response status 502
WARNING: Data received for https://wdq.wmflabs.org/api?q=CLAIM%5B462%3A1090%5D but no JSON could be decoded: '<html>\r\n<head><title>502 Bad Gateway</title></head>\r\n<body bgcolor="white">\r\n<center><h1>502 Bad Gateway</h1></center>\r\n<hr><center>nginx/1.11.3</center>\r\n</body>\r\n</html>\r\n'
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
/srv/paws/pwb/pywikibot/data/wikidataquery.py in getDataFromHost(self, queryStr)
    596         try:
--> 597             data = json.loads(data)
    598         except ValueError:

/usr/lib/python3.4/json/__init__.py in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
    317             parse_constant is None and object_pairs_hook is None and not kw):
--> 318         return _default_decoder.decode(s)
    319     if cls is None:

/usr/lib/python3.4/json/decoder.py in decode(self, s, _w)
    342         """
--> 343         obj, end = self.raw_decode(s, idx=_w(s, 0).end())
    344         end = _w(s, end).end()

/usr/lib/python3.4/json/decoder.py in raw_decode(self, s, idx)
    360         except StopIteration as err:
--> 361             raise ValueError(errmsg("Expecting value", s, err.value)) from None
    362         return obj, end

ValueError: Expecting value: line 1 column 1 (char 0)

During handling of the above exception, another exception occurred:

ServerError                               Traceback (most recent call last)
<ipython-input-14-8c79044817c7> in <module>()
     28 for key in error_dict:
     29     wdq = "CLAIM[{}:{}]".format(property[1:], key[1:])
---> 30     generator = pg.WikidataQueryPageGenerator(wdq, site)
     31     generator = site.preloadpages(generator, pageprops=True)
     32     correct_claim(generator, key)

/srv/paws/pwb/pywikibot/pagegenerators.py in WikidataQueryPageGenerator(query, site)
   2700 
   2701     wd_query = wdquery.WikidataQuery(cacheMaxAge=0)
-> 2702     data = wd_query.query(wd_queryset)
   2703     # This item count should not be copied by other generators,
   2704     # and should be removed when wdq becomes a real generator (T135592)

/srv/paws/pwb/pywikibot/data/wikidataquery.py in query(self, q, labels, props)
    622         # the cached data must not be OK, go and get real data from the
    623         # host's API
--> 624         data = self.getDataFromHost(fullQueryString)
    625 
    626         # no JSON found

/srv/paws/pwb/pywikibot/data/wikidataquery.py in getDataFromHost(self, queryStr)
    602             raise pywikibot.ServerError(
    603                 'Data received for %s but no JSON could be decoded: %r'
--> 604                 % (url, data))
    605 
    606         return data

ServerError: Data received for https://wdq.wmflabs.org/api?q=CLAIM%5B462%3A1090%5D but no JSON could be decoded: '<html>\r\n<head><title>502 Bad Gateway</title></head>\r\n<body bgcolor="white">\r\n<center><h1>502 Bad Gateway</h1></center>\r\n<hr><center>nginx/1.11.3</center>\r\n</body>\r\n</html>\r\n'