Example #1
0
 def _build_selector(cls, tree, selector_type):
     if selector_type == 'xpath':
         return XpathSelector(tree)
     elif selector_type == 'json':
         return JsonSelector(tree)
     else:
         raise GrabMisuseError('Unknown selector type: %s' % selector_type)
Example #2
0
 def test_select_node(self):
     self.assertEquals({
         'name': 'Mars',
         'cities': [],
         'population': 0
     },
                       JsonSelector(self.tree).select('$[1]')[0].node.value)
Example #3
0
 def test_attr_list(self):
     root = JsonSelector(self.tree)
     self.assertRaises(
         NotImplementedError,
         lambda: root.select('$..population').attr_list('bar'))
Example #4
0
 def test_html(self):
     root = JsonSelector(self.tree)
     self.assertRaises(NotImplementedError,
                       lambda: root.select('$..population')[0].html())
Example #5
0
 def test_text_list(self):
     root = JsonSelector(self.tree)
     self.assertEquals(['7000000000', '0'],
                       root.select('$..population').text_list())
Example #6
0
 def test_select_select(self):
     root = JsonSelector(self.tree)
     self.assertEquals('Mars', root.select('$[1]').select('name').text())
Example #7
0
 def test_population(self):
     self.assertEquals(
         7000000000,
         JsonSelector(self.tree).select('$..population').number())
Example #8
0
 def test_text(self):
     self.assertEquals('Mars',
                       JsonSelector(self.tree).select('$[1].name').text())
Example #9
0
 def test_html(self):
     sel = JsonSelector(self.tree)
     self.assertRaises(NotImplementedError, sel.html)
Example #10
0
 def test_it_works(self):
     sel = JsonSelector(self.tree)
Example #11
0
 def test_attr_list(self):
     root = JsonSelector(self.tree)
     self.assertRaises(NotImplementedError,
         lambda: root.select('$..population').attr_list('bar'))
Example #12
0
 def test_html(self):
     root = JsonSelector(self.tree)
     self.assertRaises(NotImplementedError,
         lambda: root.select('$..population')[0].html())
Example #13
0
 def test_text_list(self):
     root = JsonSelector(self.tree)
     self.assertEquals(['7000000000', '0'],
                       root.select('$..population').text_list())
Example #14
0
 def test_select_select(self):
     root = JsonSelector(self.tree)
     self.assertEquals('Mars', root.select('$[1]').select('name').text())