-
Notifications
You must be signed in to change notification settings - Fork 0
/
scrapePage.py
179 lines (152 loc) · 5.5 KB
/
scrapePage.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
import urllib2
import chardet
from time import sleep
from BeautifulSoup import BeautifulSoup
def openStoryPage(url):
req = urllib2.Request(url)
for i in range(3):
try:
response = urllib2.urlopen(req, timeout=10)
except:
if i == 2: return None
sleep(10)
if response.getcode() == 200:
try:
page = response.read()
encoding = chardet.detect(page)
page = page.decode(encoding['encoding'], errors='ignore')
page = page.encode("ascii", errors='ignore')
except:
print "Excepted"
response.close()
return None
if page.find("FanFiction.Net Message Type 1") != -1:
print "Nothing here"
response.close()
return None
else:
print "Response code fuckup"
response.close()
return None
response.close()
return page
from .fanfictionClasses import *
def scrapePage(page_URL, id):
req = urllib2.Request(page_URL)
for i in range(3):
try:
response = urllib2.urlopen(req, timeout=10)
except:
if i == 2: return None
sleep(10)
if response.getcode() == 200:
try:
page = response.read()
encoding = chardet.detect(page)
page = page.decode(encoding['encoding'], errors='ignore')
page = page.encode("ascii", errors='ignore')
except:
response.close()
return None
if "User is no longer an active member." in page or "User does not exist or is no longer an active member." in page:
response.close()
return None
else:
response.close()
return None
response.close()
writtenStart = "<div class='z-list mystories"
favoriteStart = "<div class='z-list favstories'"
endString = "</div></div></div>"
numWritten = page.count(writtenStart)
numFavorite = page.count(favoriteStart)
if numWritten == 0 and numFavorite == 0: return None
authorfind = """<span style="font-weight:bold;letter-spacing:1px;font-size:18px;font-family:'Georgia','Times New Roman','Times', Sans-serif;">"""
authfindindex = page.index(authorfind)
authorName = page[authfindindex+len(authorfind)+1:page.find("</span>", authfindindex)]
author = Author(id, authorName)
prevIndex = 0
index = page.index
a_s = author.add_story
a_f = author.add_favorite
for i in range(numWritten):
start = index(writtenStart, prevIndex)
end = index(endString, start)
prevIndex = end
exert = page[start:end]
exert = exert.replace("<>", "")
a = exert.index("data-title")
b = exert.index("data-wordcount")
datatitles = exert[a:b]
datatitles = "%s\"" % datatitles.replace("<", "").replace(">", "").replace("\"", "").replace("data-title=", "data-title=\"")
exert = "%s%s%s" % (exert[:a],datatitles,exert[b:])
a_s(Story.fromURL(exert))
prevIndex = 0
for i in range(numFavorite):
start = index(favoriteStart, prevIndex)
end = index(endString, start)
prevIndex = end
exert = page[start:end]
exert = exert.replace("<>", "").replace("\"\"", "\"")
a = exert.index("data-title")
b = exert.index("data-wordcount")
datatitles = exert[a:b]
datatitles = "%s\"" % datatitles.replace("<", "").replace(">", "").replace("\"", "").replace("data-title=", "data-title=\"")
exert = "%s%s%s" % (exert[:a],datatitles,exert[b:])
a_f(Story.fromURL(exert))
return author
def openReviewPage(url):
req = urllib2.Request(url)
for i in range(3):
try:
response = urllib2.urlopen(req, timeout=10)
except:
if i == 2: return None
sleep(10)
if response.getcode() == 200:
try:
page = response.read()
encoding = chardet.detect(page)
page = page.decode(encoding['encoding'], errors='ignore')
page = page.encode("ascii", errors='ignore')
except:
print "Excepted"
response.close()
return None
if page.find("Story not found.") != -1 and page.find("No Reviews found.") != -1:
print "Nothing here"
response.close()
return None
else:
print "Response code fuckup"
response.close()
return None
response.close()
return page
def scrapeReview(storyID):
reviews = []
page = openReviewPage("http://www.fanfiction.net/r/%d/0/1" % storyID)
if page == None: return []
check = page.find("/'>Last</a>")
if check != -1:
totalPages = int(page[page.rfind("/", 0, check)+1:check])
else:
totalPages = 1
#print totalPages
page = None
for q in range(totalPages):
while page == None:
page = openReviewPage("http://www.fanfiction.net/r/%d/0/%d" % (storyID, q+1))
end = page.find("id='gui_table1i'")
end = page.find("tbody", end)
numReviews = page.count("<tr >", end)
#print end
for i in range(numReviews):
start = page.find("<tr >", end)
end = page.find("</tr>", start)
#print start
#print end
exert = page[start:end]
#print exert
reviews.append(Review(exert, storyID))
return reviews