import pywikibot
from pywikibot import pagegenerators

maxwrites=1
wdtxt='<!-- zie wikidata -->'
site=pywikibot.Site('nl','wikipedia')
template='template:Infobox tennisspeler'

def alle_tennisspelers():
    sjP=pywikibot.Page(pywikibot.Link(template,site))
    for a_page in pagegenerators.ReferringPageGenerator(sjP): 
      if (a_page.namespace().id in [0]):   
        yield(a_page)

def get_parameter(fulltext,text):
  i=0
  j=1
  while (i<len(text)) and (text[i:i+1]!='='):   #find start of value
    i+=1
  while (i+j<len(text)) and (text[i+j:i+j+1] not in ['|','=','{','}','\n',]):   #find end of value
    if (text[i+j:i+j+2]=='[['): #it's a wikilink
      j+=2
      while (text[i+j:i+j+2]!=']]'):
        j+=1
    elif (text[i+j:i+j+4]=='<!--'): #it's a comment
      while (text[i+j:i+j+3]!='-->'):
        j+=1
    else:
      j+=1
  retval=text[i+1:i+j].replace(wdtxt,'').strip()
  if (retval[0:4]=='<!--') and (retval[len(retval)-3:len(retval)]=='-->'):
     retval=''   
  else:
    if (retval!=''):
      fulltext=fulltext.replace(retval,wdtxt,1)
  return(fulltext,retval)

def find_infoboxfield(pagetxt, search):        
  retval=''
  foundpos=pagetxt.find(search)
  if (foundpos>0) and (pagetxt[foundpos-1:foundpos] in [' ','|']):
    pagetxt,retval=get_parameter(pagetxt,pagetxt[foundpos:])
  return pagetxt,retval  
    
print('Begonnen')
pageswritten=0
for speler in alle_tennisspelers():
    page=speler #pywikibot.Page(site,speler)
    page.text,er=find_infoboxfield(page.text,'Enkelrecord')
    page.text,dr=find_infoboxfield(page.text,'Dubbelrecord')
    page.text,pg=find_infoboxfield(page.text,'prijzengeld')
    if ((pg+dr+er).strip()!=''):
      pywikibot.Page(site,page.title()).put(page.text,summary=wdtxt)
      #print(page.title())
      pageswritten+=1
    if (pageswritten>=maxwrites):    
      break  
print(f'Pagina\'s bijgewerkt: {pageswritten}')
Begonnen
Pagina's bijgewerkt: 0