MCPcopy Create free account
hub / github.com/gawel/pyquery / __init__

Method __init__

pyquery/pyquery.py:165–252  ·  view source on GitHub ↗
(self, *args, **kwargs)

Source from the content-addressed store, hash-verified

163 _translator_class = JQueryTranslator
164
165 def __init__(self, *args, **kwargs):
166 html = None
167 elements = []
168 self._base_url = None
169 self.parser = kwargs.pop('parser', None)
170
171 if 'parent' in kwargs:
172 self._parent = kwargs.pop('parent')
173 else:
174 self._parent = no_default
175
176 if 'css_translator' in kwargs:
177 self._translator = kwargs.pop('css_translator')
178 elif self.parser in ('xml',):
179 self._translator = self._translator_class(xhtml=True)
180 elif self._parent is not no_default:
181 self._translator = self._parent._translator
182 else:
183 self._translator = self._translator_class(xhtml=False)
184
185 self.namespaces = kwargs.pop('namespaces', None)
186
187 if kwargs:
188 # specific case to get the dom
189 if 'filename' in kwargs:
190 html = open(kwargs['filename'],
191 encoding=kwargs.get('encoding'))
192 elif 'url' in kwargs:
193 url = kwargs.pop('url')
194 if 'opener' in kwargs:
195 opener = kwargs.pop('opener')
196 html = opener(url, **kwargs)
197 else:
198 html = url_opener(url, kwargs)
199 if not self.parser:
200 self.parser = 'html'
201 self._base_url = url
202 else:
203 raise ValueError('Invalid keyword arguments %s' % kwargs)
204
205 elements = fromstring(html, self.parser)
206 # close open descriptor if possible
207 if hasattr(html, 'close'):
208 try:
209 html.close()
210 except Exception:
211 pass
212
213 else:
214 # get nodes
215
216 # determine context and selector if any
217 selector = context = no_default
218 length = len(args)
219 if length == 1:
220 context = args[0]
221 elif length == 2:
222 selector, context = args

Callers

nothing calls this directly

Calls 4

_css_to_xpathMethod · 0.95
url_openerFunction · 0.85
fromstringFunction · 0.85
extendMethod · 0.80

Tested by

no test coverage detected