0

I use PywikiBot core version in linux to create a program to simply get categories of a Wikipedia page.my code is:

# -*- coding: utf-8  -*-
import pywikibot

site = pywikibot.Site("en")
page = pywikibot.Page(site, u"Wikipedia:Sandbox")

item = pywikibot.ItemPage.fromPage(page)
dictionary = item.get()

print page.categories

And i expect to get categories but i get :

<bound method Page.categories of Page(Wikipedia:Sandbox)>

I follow this tutorial but i should say documentation in pywikibot is badly written and you should open files to find out some information and i found def categories:

def categories(self, withSortKey=False, step=None, total=None,
               content=False):
    """Iterate categories that the article is in.

    @param withSortKey: if True, include the sort key in each Category.
    @param step: limit each API call to this number of pages
    @param total: iterate no more than this number of pages in total
    @param content: if True, retrieve the content of the current version
        of each category description page (default False)
    @return: a generator that yields Category objects.

    """
    return self.site.pagecategories(self, withSortKey=withSortKey,
                                    step=step, total=total, content=content)

And i prefer no to change framework codes.

1 Answers1

4

Try this:

print page.categories()

Edit:

I test this before i get :<pywikibot.data.api.CategoryPageGenerator object at 0xb6c444ec> 

That is because the method returns a generator, which needs to be iterated before it gets the data. When using list(page.categories()) it creates a list from the generator.

Another, and preferred way, is to use the generator in a for loop, like this:

for category in page.categories():
    print category

You can read about generators here:

https://wiki.python.org/moin/Generators

scandinavian_
  • 2,496
  • 1
  • 17
  • 19