from readwikidata import get wikipedia data python

Solutions on MaxInterview for from readwikidata import get wikipedia data python by the best coders in the world

showing results for - "from readwikidata import get wikipedia data python"
Fleur
04 Jun 2020
1import pywikibot
2
3from pywikibot import pagegenerators as pg
4
5def list_template_usage(site_obj, tmpl_name):
6    """
7    Takes Site object and template name and returns a generator.
8
9    The function expects a Site object (pywikibot.Site()) and
10    a template name (String). It creates a list of all
11    pages using that template and returns them as a generator.
12    The generator will load 50 pages at a time for iteration.
13    """
14    name = "{}:{}".format(site_obj.namespace(10), tmpl_name)
15    tmpl_page = pywikibot.Page(site_obj, name)
16    ref_gen = pg.ReferringPageGenerator(tmpl_page, onlyTemplateInclusion=True)
17    filter_gen = pg.NamespaceFilterPageGenerator(ref_gen, namespaces=[0])
18    generator = site_obj.preloadpages(filter_gen, pageprops=True)
19    return generator
20
21site = pywikibot.Site("en", 'wikipedia')
22tmpl_gen = list_template_usage(site, "Infobox meteorite")
23
24for page in tmpl_gen:
25    item = pywikibot.ItemPage.fromPage(page)
26    print(page.title(), item.getID())
27