Wikidata:Pywikibot - Python 3 Tutorial/Setting qualifiers
This chapter will introduce the concept of adding, removing and modifying qualifiers.
Danger Zone edit
Make sure before running the examples that the bot is editing test.wikidata.org
.
Check your user-config.py
family = 'wikidata'
mylang = 'test'
usernames['wikidata']['test'] = u'YOUR_BOT_OR_USER_NAME'
Check your login (note the wikidata:test
):
$ python3 pwb.py login
Logged in on wikidata:test as YOUR_BOT_OR_USER_NAME.
Then make sure that each script calls the test-site:
site = pywikibot.Site("test", "wikidata")
All examples use the Wikidata Sandbox (Q4115189) item to further prevent accidental vandalism to Wikidata-proper by people copy-pasting code. Because the item does not exist on test.wikidata you can just create a new item (https://test.wikidata.org/wiki/Special:NewItem) for your practice edits. If you copy-paste the examples and run them, two things might happen: You will edit Wikidata Sandbox (Q4115189) or you will see an error message of test.wikidata that tells you that the item does not exist on that site.
pywikibot.data.api.APIError: no-such-entity: Could not find such an entity (Can't access entity Q4115189, revision may have been deleted.) [help:See https://test.wikidata.org/w/api.php for API usage; messages:[{'parameters': [], 'html': {'*': 'Could not find such an entity'}, 'name': 'wikibase-api-no-such-entity'}]]
ID's of properties and values do differ between test and live, so unexpected errors (like: ValueError: wikidata:test:Q101352 is not type <class 'str'>.) may arise if you use property and qualifier ID's from live.
Simple Example edit
# -*- coding: utf-8 -*-
import pywikibot
"""
Adding a qualifier to newly-created claim/statement
"""
site = pywikibot.Site("test", "wikidata")
repo = site.data_repository()
item = pywikibot.ItemPage(repo, "Q210194")
#CLAIM
claim = pywikibot.Claim(repo, u'P131') #Adding located in the administrative territorial entity (P131)
target = pywikibot.ItemPage(repo, u"Q4546") #Connecting P131 with Cambridge (Q350), who is a Q-id.
claim.setTarget(target) #Set the target value in the local object.
item.addClaim(claim, summary=u'Adding claim to Q210194') #Inserting value with summary to Q210194
#QUALIFIER
qualifier = pywikibot.Claim(repo, u'P100')
target = pywikibot.ItemPage(repo, "Q35409")
qualifier.setTarget(target)
claim.addQualifier(qualifier, summary=u'Adding a qualifier.')
Simple example on how to use addQualifier()
on a newly-created claim/statement. Make your own tests to make sure that it is added right.
Adding qualifier to existing statements/claims edit
# -*- coding: utf-8 -*-
import pywikibot
"""
Adding a qualifier to existing claims/statements
"""
site = pywikibot.Site("test", "wikidata")
repo = site.data_repository()
item = pywikibot.ItemPage(repo, "Q210194")
item.get() #Fetch all page data, and cache it.
for claim in item.claims['P131']: #Finds all statements (P131)
if 'P100' not in claim.qualifiers: #If not already exist
# Generate QUALIFIER FOR EXISTENCE STATEMENTS/CLAIMS
qualifier = pywikibot.Claim(repo, u'P100')
target = pywikibot.ItemPage(repo, "Q35409")
qualifier.setTarget(target)
claim.addQualifier(qualifier, summary=u'Adding a qualifier.') #Adding qualifier to all statements (P131)
Remove qualifier from statements/claims edit
With removeQualifier() edit
# -*- coding: utf-8 -*-
import pywikibot
"""
Remove a qualifier from claims/statements
"""
site = pywikibot.Site("test", "wikidata")
repo = site.data_repository()
item = pywikibot.ItemPage(repo, "Q210194")
item.get() #Fetch all page data, and cache it.
for claim in item.claims['P131']: #Finds all statements (P131)
if 'P100' in claim.qualifiers: #If has the qualifier we want to remove
for qual in claim.qualifiers['P100']: #iterate over all P100
claim.removeQualifier(qual, summary=u'Remove qualifier.') #remove P100
With removeQualifiers() edit
# -*- coding: utf-8 -*-
import pywikibot
"""
Remove a qualifiers form claims/statements
"""
site = pywikibot.Site("test", "wikidata")
repo = site.data_repository()
item = pywikibot.ItemPage(repo, "Q210194")
item.get() #Fetch all page data, and cache it.
#DON'T program like this. It's just to show the function 'removeQualifiers()'.
claim = item.claims['P115'][0] #Claim
qual_1 = claim.qualifiers[u'P580'][0] #qualifier
qual_2 = claim.qualifiers[u'P88'][0] #qualifier
claim.removeQualifiers([qual_1, qual_2], summary=u'Removes 2 qualifiers.') #Remove both in ONE edit. Uses list.