Manual:Pywikibot/Pywikibot and PAWS
Using Pywikibot with PAWS
Pywikibot and PAWS
editIn this tutorial you'll learn about how to start using Pywikibot using Python 3 notebooks or the terminal in PAWS.
See PAWS documentation on Wikitech for more information and tutorials.
Python 3 Notebook or Terminal?
editFrom your PAWS control panel, you have two options that make it possible for you to work with the Pywikibot library, Python 3 Notebooks and the terminal available in PAWS. This tutorial will cover the basics of each.
Before You Start
edit- Determine whether PAWS is right for your project.
- Sign up for a Wikipedia account.
- Note: Wikipedia accounts now work on all of Wikipedia's sister sites (Wikimedia, Wikitech, etc.) meaning a single account can be used.
- Read the Getting Started with PAWS tutorial.
- Use Test Wikipedia to test scripts before running them on live wikis.
- (For notebook users) Ensure that the cells of your notebook will run in the correct order.
- Set up a
user-config.py
file if you are using Pywikibot from the terminal.
Using Python 3 Notebook to Work with Pywikibot in PAWS
editThe below steps will demonstrate how to perform a few simple tasks using Pywikibot in a Python 3 notebook in PAWS. No additional software or files are necessary.
Note: If you are following this tutorial, please use Test Wikipedia to ensure that you don't inadvertently make mistakes on your wiki.
Get Started With a Python 3 Notebook
edit- Launch PAWS in your browser.
- Create a new Python 3 notebook from the control panel
- Now the Pywikibot library can be imported. In the code cell, enter the following code then click run:
import pywikibot
- Next, you will need to connect Pywikibot to the wiki you want to work with. For our tutorial we will connect to the Test Wikipedia page. No login credentials are required because you have already logged into PAWS using OAUTH. To connect to the wiki you want to work with, you will need to create an APISite object that includes the
language
andfamily
of your wiki. In the code cell, enter the following code then click run:
site = pywikibot.Site('test', 'wikipedia')
- You are now connected to the Test Wikipedia and can begin to perform basic tasks using Pywikibot.
- Later, to connect to a different wiki, you can use the code above and swap out the "language" and "family" parameters respectively. For example, to connect to the English Wikipedia, enter the following code in the code cell and click run:
site = pywikibot.Site('en', 'wikipedia') site
- The code above should produce the following output.
APIsite("en", "wikipedia")
Some Basic Tasks That Can Be Performed Using Python 3 Notebooks
editCreate a page
editsite = pywikibot.Site('test', 'wikipedia') page = pywikibot.Page(site, 'Test:Pegasus') page.save('test edit')
This code will output the following when run:
Page [[Test:Pegasus]] saved
Fetch a Page
editpage = pywikibot.Page(site, '<code>Page name</code>')
This will fetch a page called "Test:Pegasus" from the Test Wikipedia site.
After a page has been fetched, page.exists()
can be typed into the cell and will output the following:
True
In the case where the page does not exist, false
will be returned instead.
Add Text to a Page
editPywikibot can be used to add text to a page as well.
page = pywikibot.Page(site, 'Test:Pegasus') page.text = 'A pegasus is a flying horse.' page.save('test edit')
This code will output the following (or similar):
Sleeping for 9.3 seconds, 2020-10-09 17:05:14 Page [[Test:Pegasus]] saved
The text of the page can then be obtained by running page.text
in the cell. When run, page.text
will provide the following output.
'A pegasus is a flying horse.'
The examples above run in separate cells, but multiple lines can be run at once. For example, the following code...
import pywikibot site = pywikibot.Site('test', 'wikipedia') page = pywikibot.Page(site, 'test') page.text = 'Hello world!' page.save('test edit') page.text
will produce the following output (or similar):
Sleeping for 9.3 seconds, 2020-10-09 17:05:24 Page [[Test]] saved 'Hello world!'
Using a Terminal to work with Pywikibot in PAWS
editThis tutorial demonstrates how to perform a simple task using Pywikibot in a terminal in PAWS.
Note: If you are following this tutorial, please use Test Wikipedia to ensure that you don't inadvertently make mistakes on your wiki.
Set up user-config.py
editWhen you are ready to work with a wiki, you'll need to connect it to Pywikibot by setting up a user-config.py
file that connects Pywikibot to Test Wikipedia. You can explore more examples of user-config.py
files to gain a better understanding of what they are and how to set them up. You may also want to look up the code for the language of the wiki you plan to work with.
The below steps set up a basic user-config.py
file for working with PAWS.
- Create a new text file from the control panel
- Give the
user-config.py
a title.
- Add the code to indicate the language and family of your document, as well as the bot's username. See the code below.
mylang = 'test' family = 'wikipedia' usernames['wikipedia']['test'] = 'BOTNAME'
- Under the File tab click Save.
The user-config.py
file should now be visible in the index files of the PAWS control panel. This file can be altered any time.
Note: When you are working with the terminal and PAWS and wish to use a different wiki, make sure to change your user-config.py
to reflect this.
For example, if you want to work with the English Wikipedia, the user-config.py
will include:
mylang = 'en' family = 'wikipedia'
If you want to work with the Wikimedia Commons, the user-config.py
will include:
mylang = 'commons' family = 'commons'
You can find more information about the user-config.py
file in the Pywikibot manual on Mediawiki.
Get Started with the PAWS Terminal
editCreating a new Terminal
edit- Create a new terminal from the control panel
- You will be taken to a terminal.
If you wish to see the commands available, type ls /bin/
into the terminal, then hit Enter.
ls /bin/
will produce the following output.
bash* date* lessecho* pwd* uname* bunzip2* dd* lessfile@ rbash@ uncompress* bzcat* df* lesskey* readlink* vdir* bzcmp@ dir* lesspipe* rm* wdctl* bzdiff* dmesg* ln* rmdir* which* bzegrep@ dnsdomainname@ login* rnano@ ypdomainname@ bzexe* domainname@ ls* run-parts* zcat* bzfgrep@ echo* lsblk* sed* zcmp* bzgrep* egrep* mkdir* sh@ zdiff* bzip2* false* mknod* sh.distrib@ zegrep* bzip2recover* fgrep* mktemp* sleep* zfgrep* bzless@ findmnt* more* stty* zforce* bzmore* grep* mount* su* zgrep* cat* gunzip* mountpoint* sync* zless* chgrp* gzexe* mv* tar* zmore* chmod* gzip* nano* tempfile* znew* chown* hostname* nisdomainname@ touch* cp* kill* pidof@ true* dash* less* ps* umount*
Log in to Test Wikipedia
edit- Type the following text into the terminal. Login credentials are not necessary because you have already logged into PAWS using OAUTH.
pwb.py login
- The terminal should now indicate that you are logged in to Test Wikipedia.
Some Basic Tasks That Can Be Performed Using the PAWS Terminal
editCreate a Page
editThe following example will create a personal User Talk page on the Test Wiki.
- Type the following into the terminal, replacing
<username>
with your own username (created above).
pwb.py add_text -up -talk -page:"User talk:<username>" -text:"Hello. ~~~~"
- The code will produce the following output in the terminal. An option to accept the changes is provided.
Fetch a Page
editThe following example will fetch a page from the Test Wiki and save it to your PAWS control panel.
- Type the following into the terminal:
pwb.py listpages -page:"Test:Pegasus" -save
- The code will produce the following output in the terminal:
- Once the image has been created, it will appear in the PAWS control panel as a .txt file.
- The page contents can be inspected by opening the .txt file.
Explore Pywikibot Scripts
editThis tutorial covers basic functions that can be performed with Pywikibot in PAWS. A list of more complex scripts can be found at:
Pywikibot and Wikidata
editMany people use Pywikibot to work with Wikidata. To see more about this topic, see the Wikimedia section of the Pywikibot manual on this wiki and the Pywikibot - Python 3 Tutorial on Wikidata.
Other Documentation
edit- Pywikibot - Documentation for Pywikibot is located at the Pywikibot home page Pywikibot home page on MediaWiki
- PAWS - Documentation for PAWS is located at the PAWS home page on Wikitech.
Example Notebooks
edit- Add Copyright to Items in Wikidata
- Add copyright, creator to items in Wikidata
- Add awards to Wikidata category Sports Hall of Fame
- Add references to items already in Wikidata
- Auto Wikiproject
- Add short descriptions to biographies on Wikipedia EN
- Add items to Wikidata
- Change qualifier in P39 statements - Wikidata
- Make changes to pages using MyPySQL and Pywikibot - HY Wikipedia
- Remove broken files
- Investigate bot issues
- Policy changes - ZH Wikipedia
- Teahouse archives answers
- Analyze number of new editors per month
- Categorize images after the end of Wiki Loves Love
- Clean history merge list - Wikiproject history
- Fix BDA Ids on Wikidata
- Fix titles on Wikidata
- Get articles without images
- Global replace in Wikipedia DE
- Get files with NASA image template - Commons
- Remove redirect class
- Check userpage authorship - RU Wikipedia
- Fix bad interwiki links
- Upload text
- Parse data from talk pages
- Add a property to a category - Wikidata
- Autostatus update for Wikiproject
- Batch delete and unlink images
- Identify unhelpful file names on Commons
- Bulk deprecate a template
- Bulk deprecate an index parameter
- Add statements to candidates in Canada elections - Wikidata
- Move all pages from one subcategory to another
- Create new user pages
- Redirect a talk page
- Relicense uploads to Wikimedia Commons
- Replace page text
Other Links
edit- See Pywikibot Communication for links to Pywikibot discussion groups and lists.