-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWebPageScraping.py
212 lines (162 loc) · 6.19 KB
/
WebPageScraping.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#!/usr/bin/env python
#--------------------------------------------------------------------
#
# @author Ajay Kumar <[email protected]
#
# @version 1.00
#
# @copyright 2015
#
#--------------------------------------------------------------------
import sys
import re
import os
import urllib
import urllib2
import urlparse
# Checking for OrderedDict functionality.
try:
from collections import OrderedDict
except ImportError:
try:
from ordereddict import OrderedDict
except ImportError:
sys.exit('module "OrderedDict" is missing. Please install it by running "pip install ordereddict"')
# Checking for PIL module existence.
try:
from bs4 import BeautifulSoup
except ImportError:
sys.exit('ERROR: module "BeautifulSoup4" is missing. Please install it by running "pip install BeautifulSoup4".')
class WebPageScraping:
"""Parse through provided URL to find Crossword images, clues and their
Answers
"""
def __init__(self, url):
self.url = url
self.host = self.__getUrlHost()
self.__soup = None
self.__clues = OrderedDict()
self.__ACROSS = "Across"
self.__DOWN = "Down"
self.__crosswordImage = None
def __printInfo(self, show, wordType):
"""Prints Clues """
if show:
if (wordType == self.__ACROSS.lower() or wordType == "both"):
print "\n%s\tClues" % self.__ACROSS
for c, l in (self.__clues[self.__ACROSS].items()):
print "%s\t%s" % (c, l)
if (wordType == self.__DOWN.lower() or wordType == "both"):
print "\n%s\tClues" % self.__DOWN
for c, l in (self.__clues[self.__DOWN].items()):
print "%s\t%s" % (c, l)
def __readUrlPage(self, url, **urlData):
data = None
if len(urlData) > 0:
data = urllib.urlencode(urlData)
try:
fileObj = urllib2.urlopen(url, data)
except urllib2.HTTPError, e:
sys.exit('ERROR: %s - Webpage not reachable, verify the url "%s" provided.' % (str(e.code), url))
except urllib2.URLError, e:
sys.exit('ERROR: URLError = %s' % str(e.reason))
except httplib.HTTPException, e:
sys.exit('ERROR: HTTPException = %s' % str(e.reason))
except Exception:
import traceback
sys.exit('ERROR: generic exception: %s' % traceback.format_exc())
#urlRedirected = fileObj.geturl()
#urlInfo = fileObj.info()
return fileObj.read()
def __getUrlHost(self):
parseResult = urlparse.urlparse(self.url)
return "%s://%s" % (parseResult.scheme, parseResult.hostname)
def __downloadFileFromUrl(self, url):
#path, ext = os.path.splitext(url)
#outputFileName = os.path.join(os.path.abspath(os.path.dirname(__file__)), "crossword_puzzle" + ext)
self.__readUrlPage(url) # Validate the URL link
result = urllib.urlretrieve(url)
return result[0] # Return downloaded local file path.
def __getCrosswordCluesAndImage(self):
"""This private method will read the contents of webpage and will extract
Crossword clues and image from it.
"""
if self.__soup == None:
self.__soup = BeautifulSoup(self.__readUrlPage(self.url), "html.parser")
if len(self.__clues) == 2:
return self.__clues
crosswordTableId = 'printable_puzzle'
tableTag = self.__soup.find('table', id = crosswordTableId)
if tableTag == None:
sys.exit('ERROR: Couldn\'t find the crossword clues. Either URL is incorrect or script needs to be updated to accomodate webpage changes.')
imageTag = tableTag.find("img")
if imageTag == None:
sys.exit('ERROR: Couldn\'t find the crossword image. Either URL is incorrect or script needs to be updated to accomodate webpage changes.')
imagePath = re.sub(r"\?.+", "", imageTag.get('src'))
crosswordImageUrl = urlparse.urljoin(self.host, imagePath)
self.__crosswordImage = self.__downloadFileFromUrl(crosswordImageUrl)
clueTypes = [self.__ACROSS, self.__DOWN]
for clueType in (clueTypes):
tdTag = tableTag.find(string = clueType).parent.parent
if tdTag == None:
sys.exit('ERROR: Couldn\'t find "%s" clues. Either URL provided is not a valid crossword URL or script needs to be updated to accomodate webpage changes.' % clueTypes)
isClueFound = False
for index, string in enumerate(tdTag.stripped_strings):
if index == 0 and clueType == string:
self.__clues[clueType] = {}
isClueFound = True
continue
if isClueFound and string.isdigit():
Number = string
continue
if isClueFound:
self.__clues[clueType][Number] = string.lstrip('. ').rstrip(' ')
# Sorting
self.__clues[self.__ACROSS] = OrderedDict(sorted(self.__clues[self.__ACROSS].items(), key = lambda(k,v):(int(k),v)))
self.__clues[self.__DOWN] = OrderedDict(sorted(self.__clues[self.__DOWN].items(), key = lambda(k,v):(int(k),v)))
def getClues(self, wordType = "both", show = False):
"""Get the Crossword clues. Both across and down.
Args:
wordType (Optional[str]): Value can be either "across" or "down" or none
show (Optional[bool]): Prints the clue list on console.
Returns:
OrderedDict: Dictionary of all the Crossword clues.
"""
if (len(self.__clues) == 0):
self.__getCrosswordCluesAndImage()
wordType= wordType.lower()
if (wordType == "across"):
self.__printInfo(show, wordType)
return self.__clues[self.__ACROSS]
elif (wordType == "down"):
self.__printInfo(show, wordType)
return self.__clues[self.__DOWN]
else:
self.__printInfo(show, wordType)
return self.__clues
def getCrosswordImage(self):
"""Download crossword image from provided URL and returns local path.
"""
if len(self.__clues) == 0:
self.__getCrosswordCluesAndImage()
return self.__crosswordImage
def getClueAnswers(self, clueQ, clueLength):
"""Get the answer(s) for the clue from the provide URL
Args:
clueQ (string): Clue for which you need to find the answer
show (string): Length or hint for the clue.
Returns:
List: List of all possible answers.
"""
Answers = []
page = self.__readUrlPage(self.url, clue=clueQ, answer=clueLength)
self.__soup = BeautifulSoup(page, "html.parser")
def isWords(href):
return href and re.compile("/words/").search(href)
hrefResultSet = self.__soup.find_all(href=isWords)
if len(hrefResultSet) > 0:
for aElement in hrefResultSet:
text = aElement.get_text()
if len(text) == len(clueLength):
Answers.append(text)
return Answers