Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
.. _`page-ops`:
Page operations
===============
Start by :ref:`connecting <connecting>` to your site:
>>> from mwclient import Site
>>> site = mwclient.Site(('https', 'en.wikipedia.org'))
and optionally :ref:`logging in <logging-in>`:
>>> site.login('username', 'password')
Editing or creating a page
--------------------------
To get the wikitext for a specific page:
>>> page = site.pages['Greater guinea pig']
>>> text = page.text()
If a page doesn't exist, :meth:`Page.text() <mwclient.page.Page.text>`
just returns an empty string. If you need to test the existence of the
page, use `page.exists`:
>>> page.exists
True
Edit the text as you like before saving it back to the page:
>>> page.save(text, 'Edit summary')
If the page didn't exist, it is created.
Listing page revisions
----------------------
:meth:`Page.revisions() <mwclient.page.Page.revisions>` returns a List object
that you can iterate over using a for loop. Continuation
is handled under the hood so you don't have to worry about it.
*Example:* Let's find out which users did the most number of edits to a page:
>>> users = [rev['user'] for rev in page.revisions()]
>>> unique_users = set(users)
>>> user_revisions = [{'user': user, 'count': users.count(user)} for user in unique_users]
>>> sorted(user_revisions, key=lambda x: x['count'], reverse=True)[:5]
[{'count': 6, 'user': u'Wolf12345'},
{'count': 4, 'user': u'Test-bot'},
{'count': 4, 'user': u'Mirxaeth'},
{'count': 3, 'user': u'192.251.192.201'},
{'count': 3, 'user': u'78.50.51.180'}]
*Tip:* If you want to retrieve a specific number of revisions, the
:code:`itertools.islice` method can come in handy:
>>> from datetime import datetime
>>> from time import mktime
>>> from itertools import islice
>>> for revision in islice(page.revisions(), 5):
... dt = datetime.fromtimestamp(mktime(revision['timestamp']))
... print '{}'.format(dt.strftime('%F %T'))
Other page operations
---------------------
There are many other page operations like
:meth:`backlinks() <mwclient.page.Page.backlinks>`,
:meth:`embeddedin() <mwclient.page.Page.embeddedin>`,
etc. See the :class:`API reference <mwclient.page.Page>` for more.