-
Notifications
You must be signed in to change notification settings - Fork 0
/
01-data-gathering.py
137 lines (112 loc) · 4.55 KB
/
01-data-gathering.py
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
"""
Using Beautiful Soup I will grab and store the data for each episode in Campaign 3 in a cvs
"""
# Imports
import requests
from bs4 import BeautifulSoup
import re
def data_gathering_lines():
"""
Using Beautiful Soup to grab and store the data for each episode in Campaign 2.
"""
# Check we got them
#print(links)
scripts=[]
for link in links:
URL="https://kryogenix.org/crsearch/html/"+link
page=requests.get(URL)
soup=BeautifulSoup(page.content,"html.parser")
# Grab all the lines spoken
results= soup.findAll('div',id="lines")[0].text.strip()
# Returned is a giant string... Now I want to split the string by who is talking
lines=[]
for line in results.split('#'):
line = line.replace("→\n", " ")
date_regex = r"\d+/\d+/\d+" # remove dates in the format 00/00/0000
punct_regex = r"[^0-9a-zA-Z\s]" # any non-alphanumeric chars
special_chars_regex = r"[\n+]" # any speical chars
numerical_regex = r"\d+" # any remianing digits
multiple_whitespace = " {2,}" # any 2 or more consecutive white spaces (don't strip single white spaces!)
line = re.sub(date_regex, " ", line)
line = re.sub(punct_regex, " ", line)
line = re.sub(special_chars_regex, " ", line)
line = re.sub(numerical_regex, " ", line)
line = re.sub(multiple_whitespace, " ", line)
if line=='':
pass
elif line[0] ==' ':
line = line[1:]
if line[-1] ==' ':
line=line[:-1]
lines.append(line)
scripts.append(lines)
print(scripts[0][:6])
return scripts
def data_gathering_episodes_clean():
"""
Using Beautiful Soup to grab and store the data for each episode in Campaign 2.
"""
# Check we got them
#print(links)
scripts=[]
for link in links:
URL="https://kryogenix.org/crsearch/html/"+link
page=requests.get(URL)
soup=BeautifulSoup(page.content,"html.parser")
# Grab all the lines spoken
results= soup.findAll('div',id="lines")[0].text.strip()
# Returned is a giant string... Now I want to split the string by who is talking
lines=[]
for line in results.split('#'):
line = line.replace("→\n", " ")
punct_regex = r"[^0-9a-zA-Z#\s]" # any non-alphanumeric chars
special_chars_regex = r"[\n+]" # any speical chars
multiple_whitespace = " {2,}" # any 2 or more consecutive white spaces (don't strip single white spaces!)
line = re.sub(punct_regex, " ", line)
line = re.sub(special_chars_regex, " ", line)
line = re.sub(multiple_whitespace, " ", line)
if line=='':
pass
elif line[0] ==' ':
line = line[1:]
if line[-1] ==' ':
line=line[:-1]
lines.append(line)
scripts.append('\n'.join(lines))
#print(scripts[0][:6])
return scripts
def data_gathering_episodes_dirty(links):
"""
Using Beautiful Soup to grab and store the data for each episode in Campaign 2.
"""
# Check we got them
#print(links)
scripts=[]
for link in links:
URL="https://kryogenix.org/crsearch/html/"+link
page=requests.get(URL)
soup=BeautifulSoup(page.content,"html.parser")
print(link)
# Grab all the lines spoken
results= soup.findAll('div',id="lines")[0].text.strip()
results = results.replace("→", " ")
results = results.replace("♪", " ")
results = results.replace("\u0301", " ")
results = results.replace("♫", " ")
results = results.replace("\ufffd", " ")
scripts.append(results)
return "/n/n/n".join(scripts)
URL = "https://kryogenix.org/crsearch/html/index.html"
page = requests.get(URL)
soup = BeautifulSoup(page.content, "html.parser")
# Grab the links for all campaign 3 episodes and store them into a list
results= soup.find_all('a',{'href':re.compile(r'^cr3-17')})
links=[link['href'] for link in results][::-1]
data_1 = data_gathering_episodes_dirty(links[:73])
data_2 = data_gathering_episodes_dirty(links[73:])
text_file = open('CR3-17.txt','w')
text_file.write(data_1)
text_file.close()
# text_file = open('CR_data_pt2.txt','w')
# text_file.write(data_2)
# text_file.close()