Skip to content

Instantly share code, notes, and snippets.

@jz5

jz5/Program.cs

Last active Apr 12, 2020
Embed
What would you like to do?
class Program
{
static void Main(string[] args)
{
var titles = GetTitles("Category:日本語の姓");
File.WriteAllText("日本語の姓.txt", string.Join("\r\n", titles), Encoding.UTF8);
}
private static List<string> GetTitles(string cmtitle)
{
var titles = new List<string>();
var client = new RestClient("https://ja.wikipedia.org/w/") {ThrowOnAnyError = true};
string cmcontinue = null;
do
{
var request = new RestRequest("api.php", DataFormat.Json)
.AddParameter("action", "query")
.AddParameter("list", "categorymembers")
.AddParameter("cmprop", "title") // タイトルのみ取得
.AddParameter("format", "json")
.AddParameter("cmlimit", "500")
.AddParameter("cmtitle", cmtitle);
if (cmcontinue != null)
request.AddParameter("cmcontinue", cmcontinue);
var response = client.Get(request);
var o = JObject.Parse(response.Content);
titles.AddRange(o["query"]?["categorymembers"].Select(x => x["title"].ToString()));
cmcontinue = o["continue"]?["cmcontinue"]?.ToString();
} while (cmcontinue != null);
return titles;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment