forked from grangier/python-goose
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparsers.py
More file actions
250 lines (209 loc) · 6.9 KB
/
parsers.py
File metadata and controls
250 lines (209 loc) · 6.9 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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# -*- coding: utf-8 -*-
"""\
This is a python port of "Goose" orignialy licensed to Gravity.com
under one or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.
Python port was written by Xavier Grangier for Recrutae
Gravity.com licenses this file
to you under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import lxml.html
from lxml.html import soupparser
from lxml import etree
from copy import deepcopy
from goose.text import innerTrim
from goose.text import encodeValue
import re
class Parser(object):
@classmethod
def xpath_re(self, node, expression):
regexp_namespace = "http://exslt.org/regular-expressions"
items = node.xpath(expression, namespaces={'re': regexp_namespace})
return items
@classmethod
def drop_tag(self, nodes):
if isinstance(nodes, list):
for node in nodes:
node.drop_tag()
else:
nodes.drop_tag()
@classmethod
def css_select(self, node, selector):
return node.cssselect(selector)
@classmethod
def fromstring(self, html):
html = encodeValue(html)
# remove <?xml> tag because it breaks the lxml html parser
html = re.sub(r'<\?xml version\=[\"\'][0-9]\.[0-9][\"\'] encoding\=(.*?)\?>', '', html)
self.doc = lxml.html.fromstring(html)
return self.doc
@classmethod
def nodeToString(self, node):
return etree.tostring(node)
@classmethod
def replaceTag(self, node, tag):
node.tag = tag
@classmethod
def stripTags(self, node, *tags):
etree.strip_tags(node, *tags)
@classmethod
def getElementById(self, node, idd):
selector = '//*[@id="%s"]' % idd
elems = node.xpath(selector)
if elems:
return elems[0]
return None
@classmethod
def getElementsByTag(self, node, tag=None, attr=None, value=None, childs=False):
NS = "http://exslt.org/regular-expressions"
# selector = tag or '*'
selector = 'descendant-or-self::%s' % (tag or '*')
if attr and value:
selector = '%s[re:test(@%s, "%s", "i")]' % (selector, attr, value)
elems = node.xpath(selector, namespaces={"re": NS})
# remove the root node
# if we have a selection tag
if node in elems and (tag or childs):
elems.remove(node)
return elems
@classmethod
def appendChild(self, node, child):
node.append(child)
@classmethod
def childNodes(self, node):
return list(node)
@classmethod
def childNodesWithText(self, node):
root = node
# create the first text node
# if we have some text in the node
if root.text:
t = lxml.html.HtmlElement()
t.text = root.text
t.tag = 'text'
root.text = None
root.insert(0, t)
# loop childs
for c, n in enumerate(list(root)):
idx = root.index(n)
# don't process texts nodes
if n.tag == 'text':
continue
# create a text node for tail
if n.tail:
t = self.createElement(tag='text', text=n.tail, tail=None)
root.insert(idx + 1, t)
return list(root)
@classmethod
def textToPara(self, text):
return self.fromstring(text)
@classmethod
def getChildren(self, node):
return node.getchildren()
@classmethod
def getElementsByTags(self, node, tags):
selector = ','.join(tags)
elems = self.css_select(node, selector)
# remove the root node
# if we have a selection tag
if node in elems:
elems.remove(node)
return elems
@classmethod
def createElement(self, tag='p', text=None, tail=None):
t = lxml.html.HtmlElement()
t.tag = tag
t.text = text
t.tail = tail
return t
@classmethod
def getComments(self, node):
return node.xpath('//comment()')
@classmethod
def getParent(self, node):
return node.getparent()
@classmethod
def remove(self, node):
parent = node.getparent()
if parent is not None:
if node.tail:
prev = node.getprevious()
if prev is None:
if not parent.text:
parent.text = ''
parent.text += u' ' + node.tail
else:
if not prev.tail:
prev.tail = ''
prev.tail += u' ' + node.tail
node.clear()
parent.remove(node)
@classmethod
def getTag(self, node):
return node.tag
@classmethod
def getText(self, node):
txts = [i for i in node.itertext()]
return innerTrim(u' '.join(txts).strip())
@classmethod
def previousSiblings(self, node):
nodes = []
for c, n in enumerate(node.itersiblings(preceding=True)):
nodes.append(n)
return nodes
@classmethod
def previousSibling(self, node):
nodes = []
for c, n in enumerate(node.itersiblings(preceding=True)):
nodes.append(n)
if c == 0:
break
return nodes[0] if nodes else None
@classmethod
def nextSibling(self, node):
nodes = []
for c, n in enumerate(node.itersiblings(preceding=False)):
nodes.append(n)
if c == 0:
break
return nodes[0] if nodes else None
@classmethod
def isTextNode(self, node):
return True if node.tag == 'text' else False
@classmethod
def getAttribute(self, node, attr=None):
if attr:
return node.attrib.get(attr, None)
return attr
@classmethod
def delAttribute(self, node, attr=None):
if attr:
_attr = node.attrib.get(attr, None)
if _attr:
del node.attrib[attr]
@classmethod
def setAttribute(self, node, attr=None, value=None):
if attr and value:
node.set(attr, value)
@classmethod
def outerHtml(self, node):
e0 = node
if e0.tail:
e0 = deepcopy(e0)
e0.tail = None
return self.nodeToString(e0)
class ParserSoup(Parser):
@classmethod
def fromstring(self, html):
html = encodeValue(html)
self.doc = soupparser.fromstring(html)
return self.doc