File size: 3,196 Bytes
0fc716a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "ename": "ModuleNotFoundError",
     "evalue": "No module named 'bs4'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mModuleNotFoundError\u001b[0m                       Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[1], line 2\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mrequests\u001b[39;00m\n\u001b[0;32m----> 2\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mbs4\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m BeautifulSoup\n\u001b[1;32m      3\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mos\u001b[39;00m\n",
      "\u001b[0;31mModuleNotFoundError\u001b[0m: No module named 'bs4'"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "from bs4 import BeautifulSoup\n",
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "url = \"https://genji.dl.itc.u-tokyo.ac.jp/data/info.json\"\n",
    "\n",
    "response = requests.get(url).json()\n",
    "\n",
    "selections = response[\"selections\"]\n",
    "\n",
    "for selection in selections:\n",
    "\n",
    "    members = selection[\"members\"]\n",
    "\n",
    "    for member in members:\n",
    "\n",
    "        aozora_urls = []\n",
    "\n",
    "        for metadata in member[\"metadata\"]:\n",
    "\n",
    "            if metadata[\"label\"] == \"aozora\":\n",
    "\n",
    "                aozora_urls = metadata[\"value\"].split(\", \")\n",
    "\n",
    "        for aozora_url in aozora_urls:\n",
    "\n",
    "            filename = aozora_url.split(\"/\")[-1].split(\".\")[0]\n",
    "\n",
    "            opath = f\"./data/text/{filename}.txt\"\n",
    "\n",
    "            if os.path.exists(opath):\n",
    "                continue\n",
    "                # pass\n",
    "\n",
    "            response = requests.get(aozora_url)\n",
    "\n",
    "            response.encoding = response.apparent_encoding\n",
    "\n",
    "            soup = BeautifulSoup(response.text, \"html.parser\")\n",
    "\n",
    "            div = soup.find(\"div\", class_=\"main_text\")        \n",
    "\n",
    "            txt = div.get_text().strip()\n",
    "\n",
    "            os.makedirs(os.path.dirname(opath), exist_ok=True)\n",
    "\n",
    "            with open(opath, \"w\") as f:\n",
    "                f.write(txt)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}