Mwclient
lastmod 2025-02-17created 2025-01-3111 edits
Python client library to interface with the MediaWiki API
Page
In mwclient, each property of the page object means the following:
Basic information
- namespace: the page's namespace number (e.g., 0 is a regular page, 1 is a talk page, etc.)
- name: the full name of the page (including the namespace)
- page_title: the title of the page (excluding the namespace)
- base_title: the base title of the page (for subpages, the root page title)
- base_name: similar to base_title, but may include the subpage name
Example
- When requesting the Template:Table:Android:AOSP page:
- namespace: 10
- name:
틀:Table:Android:AOSP(틀(teul) is Korean for "Template") - page_title: Table:Android:AOSP
- base_title: Table:Android:AOSP
- base_name:
틀:Table:Android:AOSP(틀(teul) is Korean for "Template")
Edit and revision information
- touched: date and time of the last modification (converted to a datetime object)
- revision: current revision ID
- exists: whether the page exists (True/False)
- length: length of the page in bytes (page size)
- redirect: whether the page is a redirect (True/False)
- page_id: unique ID of the page
Protection and edit restriction information
- protection: page protection status (e.g., edit protection, move protection, etc.)
- content_model: the page's content model (e.g., wikitext, json, Scribunto, etc.)
- page_language: the page's language setting
- restriction_types: list of restriction types applied to the page (e.g., edit, move, etc.)
Edit and revision timestamps
- edit_time: time of the last edit
- last_rev_time: time of the last revision
Getting the list of all pages
import mwclient
import markdown
import os
# MediaWiki 사이트 설정
WIKI_URL = 'your.mediawiki.url'
USERNAME = 'your_username'
PASSWORD = 'your_password'
# 저장할 디렉토리 설정
OUTPUT_DIR = "wiki_markdown"
os.makedirs(OUTPUT_DIR, exist_ok=True)
# 사이트 연결 및 로그인
site = mwclient.Site(WIKI_URL, path='/')
site.login(USERNAME, PASSWORD)
# 모든 페이지 가져오기
for page in site.allpages():
title = page.name
content = page.text()
# Markdown 변환 (기본 변환, 추가적인 변환 필요할 수 있음)
markdown_content = markdown.markdown(content)
# 파일명 정리 (파일명에 사용할 수 없는 문자 대체)
filename = title.replace("/", "_").replace(":", "_") + ".md"
filepath = os.path.join(OUTPUT_DIR, filename)
# Markdown 파일로 저장
with open(filepath, "w", encoding="utf-8") as md_file:
md_file.write(markdown_content)
print(f"변환 완료: {filename}")
Page
You can get a specific page object with page = site.pages['Python (programming language)'].
-
page.text()- get the page content -
print(page.categories())- print the page's categories -
print(page.images())- print the images included in the page -
print(page.fullurl())- get the actual URL -
file_name = urlparse(url).path.split('/')[-1]- extract only the file name from a URL -
page_id = page.pageid- get the page ID
Troubleshooting
=== WARNING Main-account login via "action=login" is deprecated and may stop working without warning ===
2025-02-04 16:02:12.595 1467510/130431506324352 mwclient.client WARNING Subscribe to the mediawiki-api-announce mailing list at <https://lists.wikimedia.org/postorius/lists/mediawiki-api-announce.lists.wikimedia.org/> for notice of API deprecations and breaking changes.
2025-02-04 16:02:12.595 1467510/130431506324352 mwclient.client WARNING Main-account login via "action=login" is deprecated and may stop working without warning. To continue login with "action=login", see [[Special:BotPasswords]]. To safely continue using main-account login, see "action=clientlogin".
See also
Favorite site
Pages that link here