本文整理汇总了Python中selection.backend.XpathSelector类的典型用法代码示例。如果您正苦于以下问题:Python XpathSelector类的具体用法?Python XpathSelector怎么用?Python XpathSelector使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了XpathSelector类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_regexp
def test_regexp(self):
html = '<div><h1 id="h1">foo</h1><h2>bar</h2></div>'
sel = XpathSelector(fromstring(html))
self.assertEqual('h2', sel.select('//*[re:test(text(), "b.r")]')\
.node().tag)
self.assertEqual('foo', sel.select('//*[re:test(@id, "^h\d+$")]'
'/text()').text())
示例2: test_incorrect_xpath
def test_incorrect_xpath(self):
# The lxml xpath function return boolean for following xpath
# This breaks selector internal logic that assumes that only
# list could be returnsed
# So it was fixed and this test was crated
sel = XpathSelector(self.tree).select('//ul/li/text()="oops"')
self.assertEquals(False, sel.exists())
# Selector list is always empty in this special case
# Even if the xpath return True on lxml level
self.assertEquals(True, self.tree.xpath('//ul[1]/li[1]/text()="one"'))
sel = XpathSelector(self.tree).select('//ul[1]/li[1]/text()="one"')
self.assertEquals(False, sel.exists())
示例3: test_rex
def test_rex(self):
sel = XpathSelector(self.tree).select("//ul/li")
self.assertTrue(isinstance(sel.rex("(\w+)"), RexResultList))
示例4: test_attr_list
def test_attr_list(self):
root = XpathSelector(self.tree)
self.assertEquals(set(["li-1", "li-2"]), set(root.select('//ul[@id="second-list"]/li').attr_list("class")))
示例5: test_attr_default
def test_attr_default(self):
sel = XpathSelector(self.tree).select("//ul[2]/li[10]")
self.assertRaises(DataNotFound, lambda: sel.attr("class"))
self.assertEquals("DEFAULT", sel.attr("class", default="DEFAULT"))
示例6: test_number
def test_number(self):
sel = XpathSelector(self.tree).select("//ul/li[4]/text()")
self.assertEquals(4, sel.rex("(\d+)").number())
示例7: test_html
def test_html(self):
sel = XpathSelector(self.tree.xpath("//h1")[0])
self.assertEquals("<h1>test</h1>", sel.html().strip())
示例8: test_one
def test_one(self):
sel = XpathSelector(self.tree).select("//ul/li")
self.assertEquals("SRE_Match", sel.rex("one").one().__class__.__name__)
示例9: test_text_no_default
def test_text_no_default(self):
sel = XpathSelector(self.tree).select("//ul/li/text()")
self.assertRaises(DataNotFound, lambda: sel.rex("(zz)").text())
示例10: test_node_default
def test_node_default(self):
sel = XpathSelector(self.tree).select("//ul/li[10]")
self.assertRaises(DataNotFound, lambda: sel.node())
self.assertEqual("DEFAULT", sel.node(default="DEFAULT"))
示例11: test_text
def test_text(self):
sel = XpathSelector(self.tree).select("//ul/li")
self.assertEquals("one", sel.text())
示例12: test_text_selector_html
def test_text_selector_html(self):
sel = XpathSelector(self.tree).select("//li/text()").one()
self.assertEquals(u"one", sel.html())
示例13: test_text_selector_select
def test_text_selector_select(self):
sel = XpathSelector(self.tree).select("//li/text()").one()
self.assertRaises(SelectionRuntimeError, lambda: sel.select("foo"))
示例14: test_rex_method
def test_rex_method(self):
sel = XpathSelector(self.tree)
self.assertTrue(isinstance(sel.select("//li").rex("\w*"), RexResultList))
示例15: test_rex_default
def test_rex_default(self):
sel = XpathSelector(self.tree).select("//ul/li[10]")
self.assertRaises(DataNotFound, lambda: sel.rex("zz"))
self.assertEquals("DEFAULT", sel.rex("zz", default="DEFAULT"))