本文整理汇总了Python中pyquery.pyquery.pq函数的典型用法代码示例。如果您正苦于以下问题:Python pq函数的具体用法?Python pq怎么用?Python pq使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了pq函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_fn_with_kwargs
def test_fn_with_kwargs(self):
"fn() with keyword arguments."
pq.fn.test = lambda p=1: pq(this).eq(p) # NOQA
S = pq(self.html)
self.assertEqual(S('li').test(0).text(), 'Coffee')
self.assertEqual(S('li').test().text(), 'Tea')
self.assertEqual(S('li').test(p=2).text(), 'Milk')
示例2: test_map
def test_map(self):
def ids_minus_one(i, elem):
return int(self.klass(elem).attr('id')[-1]) - 1
assert self.klass('div', self.html).map(ids_minus_one) == [0, 1]
d = pq('<p>Hello <b>warming</b> world</p>')
self.assertEqual(d('strong').map(lambda i, el: pq(this).text()), []) # NOQA
示例3: test_fn_with_kwargs
def test_fn_with_kwargs(self):
"fn() with keyword arguments."
pq.fn.test = lambda p=1: pq(this).eq(p)
S = pq(self.html)
self.assertEqual(S("li").test(0).text(), "Coffee")
self.assertEqual(S("li").test().text(), "Tea")
self.assertEqual(S("li").test(p=2).text(), "Milk")
示例4: test_fn
def test_fn(self):
"Example from `PyQuery.Fn` docs."
fn = lambda: this.map(lambda i, el: pq(this).outerHtml()) # NOQA
pq.fn.listOuterHtml = fn
S = pq(self.html)
self.assertEqual(S('li').listOuterHtml(),
['<li>Coffee</li>', '<li>Tea</li>', '<li>Milk</li>'])
示例5: test_replaceWith_with_function
def test_replaceWith_with_function(self):
expected = '''<div class="portlet">
TestimageMy link text
imageMy link text 2
Behind you, a three-headed HTML&dash;Entity!
</div>'''
d = pq(self.html)
d('a').replace_with(lambda i, e: pq(e).html())
val = d.__html__()
assert val == expected, (repr(val), repr(expected))
示例6: test_remove
def test_remove(self):
d = pq(self.html)
d("img").remove()
val = d("a:first").html()
assert val == "Test My link text", repr(val)
val = d("a:last").html()
assert val == " My link text 2", repr(val)
示例7: test_filter
def test_filter(self):
assert len(self.klass("div", self.html).filter(".node3")) == 1
assert len(self.klass("div", self.html).filter("#node2")) == 1
assert len(self.klass("div", self.html).filter(lambda i: i == 0)) == 1
d = pq("<p>Hello <b>warming</b> world</p>")
self.assertEqual(d("strong").filter(lambda el: True), [])
示例8: test_unicode
def test_unicode(self):
xml = pq(u("<p>é</p>", 'utf-8'))
self.assertEqual(type(xml.html()), text_type)
if PY3k:
self.assertEqual(str(xml), '<p>é</p>')
else:
self.assertEqual(unicode(xml), u("<p>é</p>", 'utf-8'))
self.assertEqual(str(xml), '<p>é</p>')
示例9: test_val_for_textarea
def test_val_for_textarea(self):
d = pq(self.html3)
self.assertEqual(d('textarea').val(), 'Spam')
self.assertEqual(d('textarea').text(), 'Spam')
d('textarea').val('42')
self.assertEqual(d('textarea').val(), '42')
# Note: jQuery still returns 'Spam' here.
self.assertEqual(d('textarea').text(), '42')
示例10: _login
def _login(self, email, password):
self._log("Logging in...")
login_page = self._session.get('https://www.fitbit.com/login')
form = pq(login_page.content).find('#loginForm')
action = form.attr('action')
data = dict((i.name, i.value) for i in form.find('input'))
data.update({'email': email, 'password': password})
self._session.post(action, data)
示例11: test_get
def test_get(self):
if not HAS_REQUEST:
return
d = pq(u('http://ru.wikipedia.org/wiki/Заглавная_страница', 'utf8'),
method='get')
print(d)
self.assertEqual(d('#n-mainpage a').text(),
u('Заглавная страница', 'utf8'))
示例12: test_val_for_multiple_elements
def test_val_for_multiple_elements(self):
d = pq(self.html5)
# "Get" returns *first* value.
self.assertEqual(d('div > *').val(), 'spam')
# "Set" updates *every* value.
d('div > *').val('42')
self.assertEqual(d('#first').val(), '42')
self.assertEqual(d('#second').val(), '42')
self.assertEqual(d('#third').val(), '42')
示例13: test_replaceWith
def test_replaceWith(self):
expected = '''<div class="portlet">
<a href="/toto">TestimageMy link text</a>
<a href="/toto2">imageMy link text 2</a>
Behind you, a three-headed HTML&dash;Entity!
</div>'''
d = pq(self.html)
d('img').replace_with('image')
val = d.__html__()
assert val == expected, (repr(val), repr(expected))
示例14: test_unicode
def test_unicode(self):
xml = pq(u("<html><p>é</p></html>", "utf-8"))
self.assertEqual(type(xml.html()), text_type)
if PY3k:
self.assertEqual(str(xml), "<html><p>é</p></html>")
self.assertEqual(str(xml('p:contains("é")')), "<p>é</p>")
else:
self.assertEqual(unicode(xml), u("<html><p>é</p></html>", "utf-8"))
self.assertEqual(str(xml), "<html><p>é</p></html>")
self.assertEqual(str(xml(u('p:contains("é")', "utf8"))), "<p>é</p>")
self.assertEqual(unicode(xml(u('p:contains("é")', "utf8"))), u("<p>é</p>", "utf8"))
示例15: test_val_for_select
def test_val_for_select(self):
d = pq(self.html4)
self.assertIsNone(d('#first').val())
self.assertEqual(d('#second').val(), 'eggs')
self.assertIsNone(d('#third').val())
d('#first').val('spam')
d('#second').val('bacon')
d('#third').val('eggs') # Selecting non-existing option.
self.assertEqual(d('#first').val(), 'spam')
self.assertEqual(d('#second').val(), 'bacon')
self.assertIsNone(d('#third').val())
d('#first').val('bacon') # Selecting non-existing option.
self.assertIsNone(d('#first').val())