-
Notifications
You must be signed in to change notification settings - Fork 1
/
biblescraper.py
192 lines (170 loc) · 5.19 KB
/
biblescraper.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#By Anish Doshi, 2/21/14. Uses beautiful soup
#(http://www.crummy.com/software/BeautifulSoup/bs4/doc/)
from bs4 import BeautifulSoup
import webbrowser
import urllib2
import csv
def writeCSV(d):
write = csv.writer(open("bibledict.csv", "a"))
for key, val in d.items():
try:
write.writerow([key, val])
except:
write.writerow([key, val.encode('utf-8')])
write.writerow(["end", "line"])
def readCSV():
read = csv.reader(open("bibledict.csv"))
dictList = []
tempDict = {}
for key, val in read:
if not key == "end":
if key == "chapter" or key == "verse" or key == "date":
tempDict[key] = int(val)
else:
tempDict[key] = val.decode("unicode_escape").encode("ascii", "ignore")
else:
dictList.append(tempDict)
tempDict = dict()
return dictList
class Page:
dataTypes = {"version" : "text", "book" : "text",
"chapter" : "int", "verse" : "int", "line" : "text", "date" : "int"}
staticIndex = 1
def __init__(self, version, book, chapter, verses, url):
self.version = version
self.book = book
self.chapter = chapter
self.verses = verses
self.url = url
def unify_verses(self, single_line=True):
if (single_line):
for verse in self.verses:
line = ""
for word in self.verses[verse]:
line += word + " "
self.verses[verse] = line[:len(line) - 1]
else:
body = ""
for verse in self.verses:
for word in self.verses[verse]:
body += word + " "
body += "\n"
return body
def text_write(self, name):
text = open(name, 'a')
text.write(self.url + "\n")
text.write(self.version + "\n")
text.write(self.book + " " + str(self.chapter) + "\n\n")
for verse in self.verses:
decoded = self.verses[verse].encode('utf-8')
text.write(str(verse) + ": " + decoded + "\n")
text.close()
def exportData(self):
dictList = []
for verseNum in self.verses:
#print self.verses[verseNum]
#self.verses[verseNum].encode('utf-8')
#print(self.verses[verseNum])
contentDict = {"version" : self.version,
"book" : self.book,
"chapter": self.chapter,
"verse" : verseNum,
"line" : self.verses[verseNum],
"date" : Page.staticIndex * 3600 * 24}
Page.staticIndex += 1
#writeCSV(contentDict)
dictList.append(contentDict)
return dictList
site = "http://www.biblegateway.com"
def openURL(url):
response = urllib2.urlopen(url)
html = response.read()
return BeautifulSoup(html)
def createPage(url):
tree = openURL(url)
# tree = BeautifulSoup(html)
verseDict = {}
first = True;
for verse in tree.find_all('p', class_="verse"):
rawtext = verse.span.text.encode('ascii','ignore')
#noPunc = rawtext.replace("\"", "").replace(",", "").replace(".", "")
letterIndex = 0
while (rawtext[letterIndex].isdigit()):
letterIndex += 1
textList = rawtext[letterIndex:]
if first:
verseDict[1] = textList
first = False
else:
verseNum = int(rawtext[:letterIndex])
verseDict[verseNum] = textList
titleInfo = tree.find('div', class_="heading passage-class-0")
bookInfo = titleInfo.h3.text.split()
if (len(bookInfo) == 3):
bookInfo = [bookInfo[0] + " " + bookInfo[1], bookInfo[2]]
elif (len(bookInfo) == 4):
bookInfo = [bookInfo[0] + " " + bookInfo[1] + " " + bookInfo[2], bookInfo[3]];
print("Successfully created " + bookInfo[0] + " - " + bookInfo[1])
version = titleInfo.p.text.encode('ascii', 'ignore')
book = bookInfo[0].encode('ascii', 'ignore')
p = Page(version, book, int(bookInfo[1]),
verseDict, url)
#p.unify_verses()
#p.text_write("data.txt")
return p
def harvestEnglishLinks():
versionsTree = openURL("http://www.biblegateway.com/versions/")
versionsTable = versionsTree.find('table', class_="infotable")
englishSection = versionsTable.find('td', text="English (EN)").parent
englishVersions = [site +
"versions/21st-Century-King-James-Version-KJ21-Bible/"]
englishSection = englishSection.next_sibling
i = 20
try:
while (not englishSection.td.has_attr("rowspan")):
#print(englishSection)
englishVersions.append(site + englishSection.td.a.get('href'))
if (i > 0):
webbrowser.open(site + englishSection.td.a.get('href'))
i -= 1
englishSection = englishSection.next_sibling
except Exception as e:
pass;
return englishVersions
def adaptiveScrape(url):
tree = openURL(url)
linkTable = linkTree.find('table', class_="infotable " +
"chapterlinks updatepref")
count = 100
for link in linkTable.find_all("a"):
if (count > 0):
address = link.get('href')
adaptivePage(site + address)
count -= 1
#BibleVersion: text
#BookName: text
#BookChapter: text
#VerseNum: int
#VerseLine: text
def adaptivePage(url):
return "lol"
def kingjames():
pages = []
linkTree = openURL("http://www.biblegateway.com/" +
"versions/21st-Century-King-James-Version-KJ21-Bible/")
# linkTree = BeautifulSoup(linkRaw)
linkTable = linkTree.find('table', class_="infotable " +
"chapterlinks updatepref")
count = 50
for link in linkTable.find_all("a"):
if (count > 0):
address = link.get('href')
pages.append(createPage("http://www.biblegateway.com" + address))
count -= 1
else:
break
#print(readCSV())
return pages
#createPage("http://www.biblegateway.com/passage/?search=1+Samuel+1&version=KJ21")
#kingjames()
#harvestEnglishLinks()