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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "metadata": {},
6
+ "source": [
7
+ "# Rick and Morty Dataset\n"
8
+ ]
9
+ },
10
+ {
11
+ "cell_type": "code",
12
+ "execution_count": 2,
13
+ "metadata": {},
14
+ "outputs": [],
15
+ "source": [
16
+ "import requests\n",
17
+ "\n",
18
+ "topic_url = \"https://rickandmorty.fandom.com/wiki/Rickipedia\"\n",
19
+ "\n",
20
+ "response = requests.get(topic_url)"
21
+ ]
22
+ },
23
+ {
24
+ "cell_type": "code",
25
+ "execution_count": 3,
26
+ "metadata": {},
27
+ "outputs": [],
28
+ "source": [
29
+ "import numpy as np\n",
30
+ "import pandas as pd"
31
+ ]
32
+ },
33
+ {
34
+ "cell_type": "code",
35
+ "execution_count": null,
36
+ "metadata": {},
37
+ "outputs": [],
38
+ "source": [
39
+ "page_contents = response.text\n",
40
+ "page_contents[:1000]"
41
+ ]
42
+ },
43
+ {
44
+ "cell_type": "code",
45
+ "execution_count": null,
46
+ "metadata": {},
47
+ "outputs": [],
48
+ "source": [
49
+ "%pip install beautifulsoup4"
50
+ ]
51
+ },
52
+ {
53
+ "cell_type": "code",
54
+ "execution_count": 5,
55
+ "metadata": {},
56
+ "outputs": [
57
+ {
58
+ "data": {
59
+ "text/plain": [
60
+ "bs4.BeautifulSoup"
61
+ ]
62
+ },
63
+ "execution_count": 5,
64
+ "metadata": {},
65
+ "output_type": "execute_result"
66
+ }
67
+ ],
68
+ "source": [
69
+ "from bs4 import BeautifulSoup\n",
70
+ "\n",
71
+ "doc = BeautifulSoup(page_contents , 'html.parser')\n",
72
+ "\n",
73
+ "type(doc)"
74
+ ]
75
+ },
76
+ {
77
+ "cell_type": "code",
78
+ "execution_count": 6,
79
+ "metadata": {},
80
+ "outputs": [
81
+ {
82
+ "data": {
83
+ "text/plain": [
84
+ "11"
85
+ ]
86
+ },
87
+ "execution_count": 6,
88
+ "metadata": {},
89
+ "output_type": "execute_result"
90
+ }
91
+ ],
92
+ "source": [
93
+ "topics_link_tags = doc.find_all('a' , {'class' : 'image link-internal'})\n",
94
+ "len(topics_link_tags)"
95
+ ]
96
+ },
97
+ {
98
+ "cell_type": "code",
99
+ "execution_count": 7,
100
+ "metadata": {},
101
+ "outputs": [],
102
+ "source": [
103
+ "episode_url = 'https://rickandmorty.fandom.com' + topics_link_tags[8]['href']\n",
104
+ "\n",
105
+ "response_episode = requests.get(episode_url)\n",
106
+ "episode_page_content = response_episode.text\n",
107
+ "\n",
108
+ "doc_episode = BeautifulSoup(episode_page_content , 'html.parser')"
109
+ ]
110
+ },
111
+ {
112
+ "cell_type": "code",
113
+ "execution_count": null,
114
+ "metadata": {},
115
+ "outputs": [],
116
+ "source": [
117
+ "doc_episode_tables = doc_episode.find_all('table')\n",
118
+ "print(doc_episode_tables[1].find_all('a'))\n",
119
+ "\n",
120
+ "b_tag = doc_episode_tables[1].find_all('b')\n",
121
+ "anchor_tag = b_tag[0].find('a')\n",
122
+ "link = 'https://rickandmorty.fandom.com' +anchor_tag['href']\n",
123
+ "print(anchor_tag[1]['title'])\n",
124
+ "\n",
125
+ "\n",
126
+ "doc_episode_title = doc_episode.find_all('span' , {'class' : 'mw-headline'})\n",
127
+ "\n",
128
+ "print(doc_episode_title[2].contents)"
129
+ ]
130
+ },
131
+ {
132
+ "cell_type": "code",
133
+ "execution_count": 15,
134
+ "metadata": {},
135
+ "outputs": [],
136
+ "source": [
137
+ "def page_transcript(link , episode_num) :\n",
138
+ " \n",
139
+ " data = []\n",
140
+ "\n",
141
+ " response_episode_page = requests.get(link)\n",
142
+ " page_content = response_episode_page.text\n",
143
+ "\n",
144
+ " doc_episode_page = BeautifulSoup(page_content , 'html.parser')\n",
145
+ " para_tags = doc_episode_page.find_all('p')\n",
146
+ "\n",
147
+ " for tag in para_tags :\n",
148
+ " try :\n",
149
+ " if tag.find('b') == None:\n",
150
+ " continue\n",
151
+ " else :\n",
152
+ " list_child = list(tag.children)\n",
153
+ " dialouge = list_child[1]\n",
154
+ " \n",
155
+ " if list_child[0].find('a') == None :\n",
156
+ " name = list_child[0].contents[0]\n",
157
+ " # print(list_child[0].contents[0])\n",
158
+ "\n",
159
+ " else :\n",
160
+ " name = list_child[0].find('a').contents[0]\n",
161
+ " # print(list_child[0].find('a').contents[0])\n",
162
+ "\n",
163
+ " except Exception as e:\n",
164
+ " continue\n",
165
+ " \n",
166
+ " data.append([episode_num , name , dialouge])\n",
167
+ " \n",
168
+ " print(data)\n",
169
+ " return data"
170
+ ]
171
+ },
172
+ {
173
+ "cell_type": "code",
174
+ "execution_count": null,
175
+ "metadata": {},
176
+ "outputs": [],
177
+ "source": [
178
+ "dataset_season = []\n",
179
+ "dataset_episode_title = []\n",
180
+ "dataset = []\n",
181
+ "\n",
182
+ "j = 0\n",
183
+ "for i in range(1 , 6):\n",
184
+ " # title = doc_episode_title[i+1].contents\n",
185
+ " # dataset_season.append(title)\n",
186
+ " table = doc_episode_tables[i]\n",
187
+ " anchor_tags = doc_episode_tables[i].find_all('a')\n",
188
+ " \n",
189
+ " for tags in anchor_tags :\n",
190
+ " \n",
191
+ " try :\n",
192
+ " dataset_episode_title.append(tags['title'])\n",
193
+ " link = 'https://rickandmorty.fandom.com' +tags['href'] + '/Transcript'\n",
194
+ " j+=1\n",
195
+ " print(link , j)\n",
196
+ " data = page_transcript(link=link , episode_num=j)\n",
197
+ " dataset.append(data)\n",
198
+ " \n",
199
+ " except Exception as e:\n",
200
+ " continue\n",
201
+ "\n",
202
+ " \n",
203
+ " \n",
204
+ "print(dataset_episode_title)\n",
205
+ "\n",
206
+ "print(dataset_season)\n",
207
+ "# dataset_episode_number = np.linspace(0 , )"
208
+ ]
209
+ },
210
+ {
211
+ "cell_type": "code",
212
+ "execution_count": null,
213
+ "metadata": {},
214
+ "outputs": [],
215
+ "source": [
216
+ "dataset[23]"
217
+ ]
218
+ },
219
+ {
220
+ "cell_type": "code",
221
+ "execution_count": 152,
222
+ "metadata": {},
223
+ "outputs": [],
224
+ "source": [
225
+ "# All content put in one p tag error\n",
226
+ "\n",
227
+ "\n",
228
+ "url_error = \"https://rickandmorty.fandom.com/wiki/Pickle_Rick/Transcript\"\n",
229
+ "\n",
230
+ "response_error = requests.get(url_error)\n",
231
+ "content_error = response_error.text\n",
232
+ "doc_episode_error = BeautifulSoup(content_error , 'html.parser')\n",
233
+ "\n",
234
+ "para_tag_error = doc_episode_error.find('p')\n",
235
+ "\n",
236
+ "text_error = para_tag_error.get_text()\n",
237
+ "text_error = text_error.rsplit(\"\\n\")\n",
238
+ "\n",
239
+ "for text_e in text_error :\n",
240
+ " try:\n",
241
+ " text_e_i = text_e.rsplit(': ')\n",
242
+ " dataset[23].append([24 , text_e_i[0] , text_e_i[1]])\n",
243
+ " except Exception as e:\n",
244
+ " continue\n",
245
+ " \n"
246
+ ]
247
+ },
248
+ {
249
+ "cell_type": "code",
250
+ "execution_count": null,
251
+ "metadata": {},
252
+ "outputs": [],
253
+ "source": [
254
+ "print(dataset[0])"
255
+ ]
256
+ },
257
+ {
258
+ "cell_type": "code",
259
+ "execution_count": null,
260
+ "metadata": {},
261
+ "outputs": [],
262
+ "source": [
263
+ "#Content put in dd tag and p tag in place of p tag error\n",
264
+ "\n",
265
+ "url_error_2 = \"https://rickandmorty.fandom.com/wiki/The_Vat_of_Acid_Episode/Transcript\"\n",
266
+ "response_error_2 = requests.get(url_error_2)\n",
267
+ "content_error_2 = response_error_2.text\n",
268
+ "doc_episode_error_2 = BeautifulSoup(content_error_2 , 'html.parser')\n",
269
+ "\n",
270
+ "dd_tag_error_2 = doc_episode_error_2.find_all('dd')\n",
271
+ "para_tag_error_2 = doc_episode_error_2.find_all('p')\n",
272
+ "print(para_tag_error_2)\n",
273
+ "# text_error_2 = para_tag_error_2.get_text()\n",
274
+ "\n",
275
+ "for t in dd_tag_error_2 :\n",
276
+ " text_error_2 = t.get_text()\n",
277
+ " # print(t.contents)\n",
278
+ " sentences = text_error_2.rsplit(\"\\n\")\n",
279
+ " for text_e_2 in sentences :\n",
280
+ " try :\n",
281
+ " print(text_e_2)\n",
282
+ " text_e_i_2 = text_e_2.rsplit(': ')\n",
283
+ " dataset[38].append([39 , text_e_i_2[0] , text_e_i_2[1]])\n",
284
+ " except Exception as e:\n",
285
+ " continue\n",
286
+ "\n",
287
+ "para_tag_error_2 = doc_episode_error_2.find_all('p')\n",
288
+ "# print(para_tag_error_2)\n",
289
+ "\n",
290
+ "for para_tags in para_tag_error_2 :\n",
291
+ " try :\n",
292
+ " para_tags_text = para_tags.get_text()\n",
293
+ " para_tags_text = para_tags_text.rsplit(\": \")\n",
294
+ " dataset[38].append([39 , para_tags_text[0] , para_tags_text[1]])\n",
295
+ " except Exception as e:\n",
296
+ " continue"
297
+ ]
298
+ },
299
+ {
300
+ "cell_type": "code",
301
+ "execution_count": null,
302
+ "metadata": {},
303
+ "outputs": [],
304
+ "source": [
305
+ "#All P tags no b tag error\n",
306
+ "\n",
307
+ "url_error_4 = \"https://rickandmorty.fandom.com/wiki/Rest_and_Ricklaxation/Transcript\"\n",
308
+ "response_error_4 = requests.get(url_error_4)\n",
309
+ "content_error_4 = response_error_4.text\n",
310
+ "doc_episode_error_4 = BeautifulSoup(content_error_4 , 'html.parser')\n",
311
+ "\n",
312
+ "para_tag_error_4 = doc_episode_error_4.find_all('p')\n",
313
+ "para_tag_error_4\n"
314
+ ]
315
+ },
316
+ {
317
+ "cell_type": "code",
318
+ "execution_count": 171,
319
+ "metadata": {},
320
+ "outputs": [],
321
+ "source": [
322
+ "\n",
323
+ "for para_tags_4 in para_tag_error_4 :\n",
324
+ " try :\n",
325
+ " para_tags_text_4 = para_tags_4.get_text()\n",
326
+ " para_tags_text_4 = para_tags_text_4.rsplit(\": \")\n",
327
+ " dataset[26].append([27 , para_tags_text_4[0] , para_tags_text_4[1]])\n",
328
+ " except Exception as e:\n",
329
+ " continue"
330
+ ]
331
+ },
332
+ {
333
+ "cell_type": "code",
334
+ "execution_count": null,
335
+ "metadata": {},
336
+ "outputs": [],
337
+ "source": [
338
+ "dataset[26]"
339
+ ]
340
+ },
341
+ {
342
+ "cell_type": "code",
343
+ "execution_count": null,
344
+ "metadata": {},
345
+ "outputs": [],
346
+ "source": [
347
+ "#Retriving missing content from other site error\n",
348
+ "\n",
349
+ "url_error_3 = \"http://rickandmorty.newtfire.org/transcripts.html\"\n",
350
+ "response_error_3 = requests.get(url_error_3)\n",
351
+ "content_error_3 = response_error_3.text\n",
352
+ "doc_episode_error_3 = BeautifulSoup(content_error_3 , 'html.parser')\n",
353
+ "\n",
354
+ "\n"
355
+ ]
356
+ },
357
+ {
358
+ "cell_type": "code",
359
+ "execution_count": null,
360
+ "metadata": {},
361
+ "outputs": [],
362
+ "source": [
363
+ "\n",
364
+ "opt_0 = doc_episode_error_3.find('div' , id = 'aboutopt26')\n",
365
+ "print(opt_0)"
366
+ ]
367
+ },
368
+ {
369
+ "cell_type": "code",
370
+ "execution_count": 158,
371
+ "metadata": {},
372
+ "outputs": [],
373
+ "source": [
374
+ "opt_0_speaker = opt_0.find_all('span' , {'class':'speaker'})\n",
375
+ "opt_0_speech = opt_0.find_all('span' , {'class':'speech'})\n",
376
+ "# print(len(opt_0_speaker))\n",
377
+ "# print(len(opt_0_speech))\n",
378
+ "for i in range (0 , len(opt_0_speaker)):\n",
379
+ " dataset[26].append([27 , opt_0_speaker[i].get_text() , opt_0_speech[i].get_text()])"
380
+ ]
381
+ },
382
+ {
383
+ "cell_type": "code",
384
+ "execution_count": null,
385
+ "metadata": {},
386
+ "outputs": [],
387
+ "source": [
388
+ "dataset"
389
+ ]
390
+ },
391
+ {
392
+ "cell_type": "code",
393
+ "execution_count": null,
394
+ "metadata": {},
395
+ "outputs": [],
396
+ "source": [
397
+ "dataset_np = dataset_np.squeeze()\n",
398
+ "dataset_np"
399
+ ]
400
+ },
401
+ {
402
+ "cell_type": "code",
403
+ "execution_count": null,
404
+ "metadata": {},
405
+ "outputs": [],
406
+ "source": [
407
+ "col = ['episode no.' , 'speaker' , 'dialouge']\n",
408
+ "\n",
409
+ "df = pd.DataFrame(dataset[0] , columns=col)\n",
410
+ "df"
411
+ ]
412
+ },
413
+ {
414
+ "cell_type": "code",
415
+ "execution_count": null,
416
+ "metadata": {},
417
+ "outputs": [],
418
+ "source": [
419
+ "for i in range(1 , 51) :\n",
420
+ " df_temp = pd.DataFrame(dataset[i] , columns=col)\n",
421
+ " df = df.append(df_temp , ignore_index = True)"
422
+ ]
423
+ },
424
+ {
425
+ "cell_type": "code",
426
+ "execution_count": null,
427
+ "metadata": {},
428
+ "outputs": [],
429
+ "source": [
430
+ "df"
431
+ ]
432
+ },
433
+ {
434
+ "cell_type": "code",
435
+ "execution_count": 196,
436
+ "metadata": {},
437
+ "outputs": [],
438
+ "source": [
439
+ "df.to_csv('Rick&Morty.csv')"
440
+ ]
441
+ },
442
+ {
443
+ "cell_type": "code",
444
+ "execution_count": null,
445
+ "metadata": {},
446
+ "outputs": [],
447
+ "source": []
448
+ }
449
+ ],
450
+ "metadata": {
451
+ "kernelspec": {
452
+ "display_name": "Python 3.10.2 64-bit",
453
+ "language": "python",
454
+ "name": "python3"
455
+ },
456
+ "language_info": {
457
+ "codemirror_mode": {
458
+ "name": "ipython",
459
+ "version": 3
460
+ },
461
+ "file_extension": ".py",
462
+ "mimetype": "text/x-python",
463
+ "name": "python",
464
+ "nbconvert_exporter": "python",
465
+ "pygments_lexer": "ipython3",
466
+ "version": "3.10.2"
467
+ },
468
+ "orig_nbformat": 4,
469
+ "vscode": {
470
+ "interpreter": {
471
+ "hash": "369f2c481f4da34e4445cda3fffd2e751bd1c4d706f27375911949ba6bb62e1c"
472
+ }
473
+ }
474
+ },
475
+ "nbformat": 4,
476
+ "nbformat_minor": 2
477
+ }