import requests
import pywikibot

query = {
    "action": "query",
    "format": "json",
    "prop": "",
    "list": "",
    "pageids": "",
    "generator": "exturlusage",
    "geuprotocol": "http",
    "geuquery": "www.lemonde.fr",
    "geulimit": "500"
}

req = requests.get("https://wikidata.org/w/api.php", params=query)
print(req.status_code)
j = req.json()

site = pywikibot.Site('wikidata', 'wikidata')
repo = site.data_repository()
print("Number of pages: " + str(len(j["query"]["pages"])))
searchindexnumber = 0
initialflag = True
for page in j["query"]["pages"]:
    searchindexnumber += 1
    qid = j["query"]["pages"][page]["title"]
    if qid.startswith("Q"):
        item = pywikibot.ItemPage(repo, qid)
        item.get()
        if item.claims:
            for claim in item.claims:
                realclaim = item.claims[claim]
                for value in realclaim:
                    for source in value.sources:
                        if "P854" in source:
                            urlclaim = source["P854"][0]
                            url = urlclaim.target
                            if "http://www.lemonde.fr" in url:
                                if initialflag:
                                    print("Search index is out of date by " + str(searchindexnumber))
                                    initialflag = False
                                print(item.id)
                                print(value.id)
                                print(url)
                                newurl = url.replace("http://", "https://")
                                print("replacing it with: " + newurl)
                                urlclaim.setTarget(newurl)
                                print(item.full_url())

                                srcclaims = []
                                for otherclaims in source.values():
                                    srcclaims.append(otherclaims[0])

                                repo.editSource(value, srcclaims, new=False, bot=False,
                                                summary="updating lemonde.fr urls to https")
200
Number of pages: 329