Manual:Pywikibot/wikipedia.py

Wikipedia.py is a module available with the pywikipedia framework. It's the most important module, because it provides most part of the normal function and it's programmed to be as easy as possibile to be used.

The most important class (and the most used) is the Page class. It provides all the common action for a wikipage. To use that, you must, first of all, call it and only after you can use its internal function.

In the example, you import the wikipedia module. After that it's needed to define the site where the page is. If you take it how it's done in the example, you'll take the page that is in the project setted in user-config.py. If you want to load the page of a different project you have to do as in the following example. At the end, there is wikipedia.stopme that is needed to exit from wikipedia's site so you don't slow down the other bot's processes.

In this example, you import wikipedia and define the site. The site, is the english wikisource because you have add the parameter 'en' and 'wikisource', so the bot won't use the default's one but what you have choosen. After that, we define the page to take (in this case, the Main Page, that we are sure that exists) and we load the text that is inside. So, to finish, we print the text to the screen to let the user to read it. After all, this code isn't so clean and good, indeed an experienced coder can do it better as in the following example.

This code is better than before for many reasons. First of all, it uses a function and it's not procedural as before. In this way, you can call the main function, only if the script is call directly. If it's not, it won't do what is written untill the coder doesn't call the main function. Furthermore, it's really better the try:/finally: block that allows to the coder to log-out from wikipedia always, even when an exception is raised. This permit not to slow down other process and to be quicker when you test a script. Another interessant new thing, is the used of wikipedia.output. It does the same job of print "something" but, and this is the important thing, it encode and decode the text using the coding defined in user-config. Note: If you don't put the text in the u"something" (with the u) the function will return an (handled, but ugly to see) error. (the text variable contain already the code encode in a good way, so it won't raise an exception).

Unfortunately, you can't always be sure that the page that you will load exists always. To prevent that wikipedia.py will raise the error NoPage