I am making a Wiki site using MediaWiki, and I searched for an API to get update information etc. in Python, but there are several Qiita articles and ** MediaWiki Japanese version with subtle translation **, English Since there was only information on MediaWiki, I decided to write an article for those who are thinking about using API in the future. This API is basically ** usable for MediaWiki sites other than Wikipedia **, so it is quite versatile. Please note that the detailed explanation may be wrong and it is a little difficult to read because it is Qiita's first post ...
These are the main parameters used to acquire the content of this article. Regarding the parameters, This Qiita article: Get Wikipedia information using MediaWiki API introduces in detail, so there are other things besides getting the article content. If you do, I think you should refer to it. Note: I can hit it from the link, but I use `` `requests``` for readability.
Parameters | Description | value | example |
---|---|---|---|
format | Output format | json,xml... | "format":"json"→ Output with json |
action | operation | query,edit... | "action":"query"→取得operationをする |
prop | Get article components | revisions,links,images... | "prop":"revisions"→ Get revision |
title | Article title | Article title | "titles":"Cat"→「Cat」という記事を取得する |
rvprop | Elements to get | content... | "rvprop":"content"→ Get the article text |
list | Get article list | categorymembers,search... | "list":"categorymembers"→ Get category members |
cmtitle | Category title | Category name | "cmtitle":"Nuko"→「Nuko」カテゴリを取得する |
cmlimit | Maximum number of acquisitions | 1~500 | "cmlimit":"100"→ Get up to 100 |
Get the article name with "Category: School Regulations of Chiba Prefecture" on the site kousokuwiki.org created by MediaWiki.
coding: UTF-8
from urllib.request import Request, urlopen
from urllib.parse import urlencode
from urllib.error import URLError, HTTPError
import json
import requests
S = requests.Session()
# Declare the URL. In the case of wikipedia, it will be "https://jp.wikipedia.org/w/api.php".
URL = "https://kousokuwiki.org/w/api.php"
# Parameter settings.
PARAMS = {
"action": "query",
"cmtitle": "Category: School Regulations in Chiba Prefecture",
"cmlimit": "500",
"list": "categorymembers",
"format": "json"
}
# Get information with get function
R = S.get(url=URL, params=PARAMS)
DATA = R.json()
# Extracting the required data from json
PAGES = DATA['query']['categorymembers']
# Extract only title information and store in list
Return_List=[]
for page in PAGES:
Return_List.append(page['title'])
print(Return_List)
[Chiba Prefectural Chiba Higashi High School Regulations, Chiba Prefectural Inba Akira Makoto High School Regulations, Chiba Prefectural Kokubun High School Regulations, Chiba Prefectural Narita International High School Regulations, Chiba Prefectural Matsudo International High School Regulations, Chiba Prefectural Kashiwanoha High School School rules ... (Omitted below)]
Get the contents of the article "School rules of Chiba Prefectural Kashiwanoha High School" on the site kousokuwiki.org created by MediaWiki.
coding: UTF-8
from urllib.request import Request, urlopen
from urllib.parse import urlencode
from urllib.error import URLError, HTTPError
import json
import requests
S = requests.Session()
# Declare the URL. In the case of wikipedia, it will be "https://jp.wikipedia.org/w/api.php".
URL = "https://kousokuwiki.org/w/api.php"
# Parameter settings.
PARAMS = {
"action": "query",
"prop": "revisions",
"titles": "School rules of Chiba Prefectural Kashiwanoha High School",
"rvprop": "content",
"format": "json"
}
# Get information with get function
R = S.get(url=URL, params=PARAMS)
DATA = R.json()
# Extracting the required data from json
CONTENT = DATA['query']['pages']
print(CONTENT)
{'61': {'pageid': 61, 'ns': 0, 'title': 'School rules of Chiba Prefectural Kashiwanoha High School', 'revisions': [{'contentformat': 'text/x-wiki', 'contentmodel': 'wikitext', '*': 'Chiba Prefectural Kashiwanoha High School(Abbreviation:Kashiwanoha High School)Is[[Chiba]]Exists in[[Public school|public]]of[[high school]]<br>\n[[Notation classification]] 、[[Disclaimer]]を確認of上ご利用ください。\n\n=Basic rules=\n\n{| class="wikitable"\n!item\n!School rules\n!Actual situation\n!Remarks\n|-\n|Use smartphone\n|Possible\n|Possible\n|PC is also possible\n|-\n|Use SNS\n|Possible\n|Possible\n| 心得of規定Isあり\n|-\n|part time job\n|Limited\n|Possible\n| 要許可願Actual situationとして成績が悪くなければ可能。シフトIs基本午後8時まで、午後10時までに帰宅することが条件(Was written on the paper).. But it doesn't matter if you don't write it on paper(店側が学校に確認of連絡を取らない場合)、時間だって虚偽of申告してもバレなかった。\n|-\n|Driver's license\n|Limited\n|Limited\n|It is possible to request permission only after deciding the course\n|-\n|School rules改正\n|Unspecified\n|impossible\n|Staff decided\n|}\n\n=Hair and dress code=\n{| class="wikitable"\n!item\n!School rules\n!Actual situation\n!Remarks\n|-\n|Men's uniform\n| colspan="3" |blazer\n|-\n|Women's uniform\n| colspan="3" |blazer+スカート、スラックス可スラックス時ofみネクタイ可\n|-\n|Shoes(Off-campus)\n| colspan="3" |black,茶of革靴又Is運動靴\n|-\n|Shoes(School)\n| colspan="3" |指定of学年色of上履き\n|-\n|Men's socks\n| colspan="3" |紺又Is白ofソックス\n|-\n|Women's socks\n|colspan="3" |black又Is紺ofハイソックス\n|-\n|back\n| colspan="3" |華美でない機能的な高校生らしいもof\n|-\n|hair\n| colspan="3" |Perm decolorization dyeing curl hair etc. prohibited\n|-\n|Coats\n|Possible\n|Possible\n|華美でないもof 校舎内でIs脱ぐ\n|-\n|Mufflers\n|Possible\n|Possible\n| 華美でないもof 校舎内でIs脱ぐ\n|-\n|Sweaters\n|Possible\n|Possible\n| 学校指定ofもof\n|-\n|make up\n|impossible\n|Escape\n|make up マニキュア ピアス カラコン 装飾品Is一切禁止\n|}\n\n=そof他規定=\n\n=Article excerpt=\n[[Category:Chibaof校則]]'}]}}
Slice only the information you want further if necessary.
I explained the MediaWiki API, which lacks Japanese information, with examples. Other parameter explanations and implementation examples may be added in the future! We hope you find it useful.
Recommended Posts