Wikidata talk:Pywikibot - Python 3 Tutorial/Iterate over a SPARQL query

Time outs edit

A lot of my query generate time-outs. Is there a way to avoid those time out errors? Edoderoo (talk) 18:22, 11 August 2016 (UTC)Reply

@Edoderoo, Yuvipanda: I think if the query times out on query.wikidata, then it will also time out for the bot. But maybe Yuvi knows more about this? --Tobias1984 (talk) 19:11, 11 August 2016 (UTC)Reply
My experience is that the web-interface gives earlier time outs. You can use LIMIT XXX to avoid this, but for my bot script this is no option, as my bot needs to pass through all the results. Edoderoo (talk) 19:17, 11 August 2016 (UTC)Reply
@Edoderoo: Try to change this setting in you user-config. I think that is what is causing the API timeouts:
# Defer bot edits during periods of database server lag.  For details, see
# https://www.mediawiki.org/wiki/Maxlag_parameter
# You can set this variable to a number of seconds, or to None (or 0) to
# disable this behavior. Higher values are more aggressive in seeking
# access to the wiki.
# Non-Wikimedia wikis may or may not support this feature; for families
# that do not use it, it is recommended to set minthrottle (above) to
# at least 1 second.
maxlag = 120

120 is working very well for me now. --Tobias1984 (talk) 04:42, 17 August 2016 (UTC)Reply

Sparql-query edit

My experience is that it is a good help to add "print(QUERY)" into the code, because sometimes the code in pka-query.rq should have an additional empty space at the end of a row. --Molarus 05:54, 1 September 2016 (UTC)Reply

alternative code to iterate a SPARQL-query edit

import pywikibot

from pywikibot import pagegenerators as pg

def wd_sparql_query(spq):
   #print(spq)
   generator=pg.WikidataSPARQLPageGenerator(spq,site=pywikibot.Site('wikidata','wikidata'))
   for wd in generator:
     try:
       wd.get(get_redirect=True)
       yield wd
     except:
       pass

query='select ?item ?wta where {?item wdt:P597 ?wta}'
site=pywikibot.Site('wikidata','wikidata')
repo=site.data_repository()

def run_query():
    for item in wd_sparql_query(query):
        act_one_item(item)

def act_one_item(item):
  pass /// do something here with the found item

run_query()
Return to the project page "Pywikibot - Python 3 Tutorial/Iterate over a SPARQL query".