Repository navigation
Expand file tree
/
Copy pathchapscrape.py
More file actions
executable file
·118 lines (94 loc) · 3.55 KB
/
Copy pathchapscrape.py
File metadata and controls
executable file
·118 lines (94 loc) · 3.55 KB
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
#!/usr/bin/python
"""
chapscrape -- script to scrape chapters for analysis
Copyright (C) 2011 Jeff Sharkey, http://jsharkey.org
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import urllib, urllib2, simplejson, sys, re
import BeautifulSoup as bs
BOOKS = [
#"John": range(1, 22)
("Hebrews", range(1, 14)),
("1 Peter", range(1, 6)),
("2 Peter", range(1, 4))
]
OUTPUTFILE = "bodytext.json"
BOOK, CHAPTER, SECTION, VERSE, TEXT = ("BOOK", "CHAPTER", "SECTION", "VERSE", "TEXT")
re_ignore = re.compile(r'The earliest manuscripts and many other ancient witnesses')
re_replace = re.compile(r'\[\[\[/woj\]\]\]')
def build_event(event, data):
return {'event':event,'data':data}
def skip_subtree(tag):
while not tag.nextSibling:
tag = tag.parent
return tag.nextSibling
output = open(OUTPUTFILE, 'w')
for book, chapters in BOOKS:
simplejson.dump([build_event(BOOK, book)], output)
output.write('\n')
for chapter in chapters:
print "scraping", book, chapter,
sys.stdout.flush()
url = "http://www.biblegateway.com/passage/?search=%s+%d&version=NIV&interface=print" % (urllib.quote_plus(book), chapter)
soup = bs.BeautifulSoup(urllib2.urlopen(url), convertEntities=bs.BeautifulSoup.ALL_ENTITIES)
start = soup.first('div', attrs={'class':'result-text-style-normal text-html '})
end = soup.first('div', attrs={'class':'footnotes'})
events = []
activeText = None
tag = start
while tag != end and tag is not None:
if isinstance(tag, bs.NavigableString):
if re_ignore.search(tag.string) is None:
string = re_replace.sub('', tag.string)
if activeText is None:
activeText = build_event(TEXT, "")
events.append(activeText)
activeText['data'] += string
if isinstance(tag, bs.Tag):
attrs = dict(tag.attrs)
if tag.name == 'div' and 'heading' in attrs['class']:
tag = skip_subtree(tag)
activeText = None
continue
elif tag.name == 'span' and 'chapternum' in attrs['class']:
events.append(build_event(CHAPTER, tag.text))
events.append(build_event(VERSE, "1"))
tag = skip_subtree(tag)
activeText = None
continue
elif tag.name == 'h3':
events.append(build_event(SECTION, tag.text))
tag = skip_subtree(tag)
activeText = None
continue
elif tag.name == 'sup' and 'versenum' in attrs['class']:
events.append(build_event(VERSE, tag.text))
tag = skip_subtree(tag)
activeText = None
continue
elif tag.name == 'sup' and 'footnote' in attrs['class']:
tag = skip_subtree(tag)
continue
elif tag.name == 'sup' and 'crossreference' in attrs['class']:
tag = skip_subtree(tag)
if isinstance(tag, bs.NavigableString):
tag.string = tag.string.lstrip()
continue
else:
pass
tag = tag.next
versecount = len([event for event in events if event['event'] is VERSE])
print "and found", versecount, "verses"
simplejson.dump(events, output) # indent=4
output.write('\n')
output.close()
print "done, scraped chapters written to", OUTPUTFILE