Manual:Pywikibot/Skriva sitt eget skript
Appearance
Pywikibot |
---|
|
Den här sidan listar några enkla tips för att komma igång med att skriva sin egen bot. Se till att du har satt upp din user-config.py
-fil!
Du kan antingen använda följande kommandon i ett skal, eller i ett skript.
To open a shell, run python pwb.py shell
.
As a script, please save the file as myscript.py
in the scripts/userscripts/
directory, then run python pwb.py myscript
.
- To gain access to the pywikibot library from your Python script, use:
import pywikibot
- to retrieve a page, use the following, where pageName is the title of the page you would like to retrieve (e.g., Wikipedia:Bots or India):
site = pywikibot.Site()
page = pywikibot.Page(site, "pageName")
text = page.text
- to update a page, use:
page.text = "newText"
page.save("Edit comment")
- look at some of the pywikibot files for other ideas --
scripts/basic.py
is relatively easy to read even if you're new to pywikibot.
- you can find all available Page methods in the
pywikibot/page.py
file.
basic.py
gives you a setup that can be used for many different bots, all you have to do is define the string editing on the page text.
- To iterate over a set of pages, see pywikibot/pagegenerators.py (see also old documentation) for some objects that return a set of pages. An example use of the CategoryPageGenerator that does something for each page in the Category:Living people category:
import pywikibot
from pywikibot import pagegenerators
site = pywikibot.Site()
cat = pywikibot.Category(site,'Category:Living people')
gen = pagegenerators.CategorizedPageGenerator(cat)
for page in gen:
#Do something with the page object, for example:
text = page.text
Se även
- Pywikibot full documentation on doc.wikimedia.org
- wikigraphviz - example of using Pywikibot with https://graphviz.org/
- How to write a basic script via Pywikibot - a small wiki toolkits workshop
- Manual:Pywikibot/Cookbook
- Om du behöver mer hjälp med att komma igång med din Pywikibot, besök IRS-kanalen #pywikibot anslut eller e-postlistan pywikibot@.