pyQuery

  

pyquery?– PyQuery complete API

選擇器基本支持jQuery用法

class?pyquery.pyquery.PyQuery(*args,?**kwargs)

The main class

class?Fn

Hook for defining custom function (like the jQuery.fn):

>>> fn = lambda: this.map(lambda i, el: PyQuery(this).outerHtml()) >>> PyQuery.fn.listOuterHtml = fn >>> S = PyQuery( ... '<ol> <li>Coffee</li> <li>Tea</li> <li>Milk</li> </ol>') >>> S('li').listOuterHtml() ['<li>Coffee</li>', '<li>Tea</li>', '<li>Milk</li>'] 
PyQuery.addClass(value)

Alias for?add_class()

PyQuery.add_class(value)

Add a css class to elements:

>>> d = PyQuery('<div></div>') >>> d.add_class('myclass') [<div.myclass>] >>> d.addClass('myclass') [<div.myclass>] 
PyQuery.after(value)

add value after nodes

PyQuery.append(value)

append value to each nodes

PyQuery.appendTo(value)

Alias for?append_to()

PyQuery.append_to(value)

append nodes to value

PyQuery.base_url

Return the url of current html document or None if not available.

PyQuery.before(value)

insert value before nodes

PyQuery.children(selector=None)

Filter elements that are direct children of self using optional selector:

>>> d = PyQuery('<span><p class="hello">Hi</p><p>Bye</p></span>') >>> d [<span>] >>> d.children() [<p.hello>, <p>] >>> d.children('.hello') [<p.hello>] 
PyQuery.clone()

return a copy of nodes

PyQuery.closest(selector=None)
>>> d = PyQuery(
... '<div class="hello"><p>This is a ' ... '<strong class="hello">test</strong></p></div>') >>> d('strong').closest('div') [<div.hello>] >>> d('strong').closest('.hello') [<strong.hello>] >>> d('strong').closest('form') [] 
PyQuery.contents()

Return contents (with text nodes):

>>> d = PyQuery('hello <b>bold</b>') >>> d.contents() ['hello ', <Element b at ...>] 
PyQuery.each(func)

apply func on each nodes

PyQuery.empty()

remove nodes content

PyQuery.encoding

return the xml encoding of the root element

PyQuery.end()

Break out of a level of traversal and return to the parent level.

>>> m = '<p><span><em>Whoah!</em></span></p><p><em> there</em></p>'
>>> d = PyQuery(m) >>> d('p').eq(1).find('em').end().end() [<p>, <p>] 
PyQuery.eq(index)

Return PyQuery of only the element with the provided index:

>>> d = PyQuery('<p class="hello">Hi</p><p>Bye</p><div></div>') >>> d('p').eq(0) [<p.hello>] >>> d('p').eq(1) [<p>] >>> d('p').eq(2) [] 
PyQuery.extend(other)

Extend with anoter PyQuery object

PyQuery.filter(selector)

Filter elements in self using selector (string or function):

>>> d = PyQuery('<p class="hello">Hi</p><p>Bye</p>') >>> d('p') [<p.hello>, <p>] >>> d('p').filter('.hello') [<p.hello>] >>> d('p').filter(lambda i: i == 1) [<p>] >>> d('p').filter(lambda i: PyQuery(this).text() == 'Hi') [<p.hello>] >>> d('p').filter(lambda i, this: PyQuery(this).text() == 'Hi') [<p.hello>] 
PyQuery.find(selector)

Find elements using selector traversing down from self:

>>> m = '<p><span><em>Whoah!</em></span></p><p><em> there</em></p>'
>>> d = PyQuery(m) >>> d('p').find('em') [<em>, <em>] >>> d('p').eq(1).find('em') [<em>] 
PyQuery.hasClass(name)

Alias for?has_class()

PyQuery.has_class(name)

Return True if element has class:

>>> d = PyQuery('<div class="myclass"></div>') >>> d.has_class('myclass') True >>> d.hasClass('myclass') True 
PyQuery.height(value=<NoDefault>)

set/get height of element

PyQuery.hide()

remove display:none to elements style

>>> print(PyQuery('<div style="display:none;"/>').hide()) <div style="display: none"/> 
PyQuery.html(value=<NoDefault>,?**kwargs)

Get or set the html representation of sub nodes.

Get the text value:

>>> d = PyQuery('<div><span>toto</span></div>') >>> print(d.html()) <span>toto</span> 

Extra args are passed to?lxml.etree.tostring:

>>> d = PyQuery('<div><span></span></div>') >>> print(d.html()) <span/> >>> print(d.html(method='html')) <span></span> 

Set the text value:

>>> d.html('<span>Youhou !</span>') [<div>] >>> print(d) <div><span>Youhou !</span></div> 
PyQuery.insertAfter(value)

Alias for?insert_after()

PyQuery.insertBefore(value)

Alias for?insert_before()

PyQuery.insert_after(value)

insert nodes after value

PyQuery.insert_before(value)

insert nodes before value

PyQuery.is_(selector)

Returns True if selector matches at least one current element, else False:

>>> d = PyQuery('<p class="hello"><span>Hi</span></p><p>Bye</p>') >>> d('p').eq(0).is_('.hello') True 
>>> d('p').eq(0).is_('span') False 
>>> d('p').eq(1).is_('.hello') False 
PyQuery.items(selector=None)

Iter over elements. Return PyQuery objects:

>>> d = PyQuery('<div><span>foo</span><span>bar</span></div>') >>> [i.text() for i in d.items('span')] ['foo', 'bar'] >>> [i.text() for i in d('span').items()] ['foo', 'bar'] >>> list(d.items('a')) == list(d('a').items()) True 
PyQuery.make_links_absolute(base_url=None)

Make all links absolute.

PyQuery.map(func)

Returns a new PyQuery after transforming current items with func.

func should take two arguments - ‘index’ and ‘element’. Elements can also be referred to as ‘this’ inside of func:

>>> d = PyQuery('<p class="hello">Hi there</p><p>Bye</p><br />') >>> d('p').map(lambda i, e: PyQuery(e).text()) ['Hi there', 'Bye'] >>> d('p').map(lambda i, e: len(PyQuery(this).text())) [8, 3] >>> d('p').map(lambda i, e: PyQuery(this).text().split()) ['Hi', 'there', 'Bye'] 
PyQuery.nextAll(selector=None)

Alias for?next_all()

PyQuery.next_all(selector=None)
>>> h = '<span><p class="hello">Hi</p><p>Bye</p><img scr=""/></span>'
>>> d = PyQuery(h) >>> d('p:last').next_all() [<img>] >>> d('p:last').nextAll() [<img>] 
PyQuery.not_(selector)

Return elements that don’t match the given selector:

>>> d = PyQuery('<p class="hello">Hi</p><p>Bye</p><div></div>') >>> d('p').not_('.hello') [<p>] 
PyQuery.outerHtml()

Alias for?outer_html()

PyQuery.outer_html()

Get the html representation of the first selected element:

>>> d = PyQuery('<div><span class="red">toto</span> rocks</div>') >>> print(d('span')) <span class="red">toto</span> rocks >>> print(d('span').outer_html()) <span class="red">toto</span> >>> print(d('span').outerHtml()) <span class="red">toto</span> >>> S = PyQuery('<p>Only <b>me</b> & myself</p>') >>> print(S('b').outer_html()) <b>me</b> 
PyQuery.parents(selector=None)
>>> d = PyQuery('<span><p class="hello">Hi</p><p>Bye</p></span>') >>> d('p').parents() [<span>] >>> d('.hello').parents('span') [<span>] >>> d('.hello').parents('p') [] 
PyQuery.prepend(value)

prepend value to nodes

PyQuery.prependTo(value)

Alias for?prepend_to()

PyQuery.prepend_to(value)

prepend nodes to value

PyQuery.prevAll(selector=None)

Alias for?prev_all()

PyQuery.prev_all(selector=None)
>>> h = '<span><p class="hello">Hi</p><p>Bye</p><img scr=""/></span>'
>>> d = PyQuery(h) >>> d('p:last').prev_all() [<p.hello>] >>> d('p:last').prevAll() [<p.hello>] 
PyQuery.remove(expr=<NoDefault>)

Remove nodes:

>>> h = '<div>Maybe <em>she</em> does <strong>NOT</strong> know</div>'
>>> d = PyQuery(h) >>> d('strong').remove() [<strong>] >>> print(d) <div>Maybe <em>she</em> does know</div> 
PyQuery.removeAttr(name)

Alias for?remove_attr()

PyQuery.removeClass(value)

Alias for?remove_class()

PyQuery.remove_attr(name)

Remove an attribute:

>>> d = PyQuery('<div id="myid"></div>') >>> d.remove_attr('id') [<div>] >>> d.removeAttr('id') [<div>] 
PyQuery.remove_class(value)

Remove a css class to elements:

>>> d = PyQuery('<div class="myclass"></div>') >>> d.remove_class('myclass') [<div>] >>> d.removeClass('myclass') [<div>] 
PyQuery.remove_namespaces()

Remove all namespaces:

>>> doc = PyQuery('<foo xmlns="http://example.com/foo"></foo>') >>> doc [<{http://example.com/foo}foo>] >>> doc.remove_namespaces() [<foo>] 
PyQuery.replaceAll(expr)

Alias for?replace_all()

PyQuery.replaceWith(value)

Alias for?replace_with()

PyQuery.replace_all(expr)

replace nodes by expr

PyQuery.replace_with(value)

replace nodes by value:

>>> doc = PyQuery("<html><div /></html>") >>> node = PyQuery("<span />") >>> child = doc.find('div') >>> child.replace_with(node) 

[<div>] >>> print(doc) <html><span/></html>

PyQuery.root

return the xml root element

PyQuery.show()

add display:block to elements style

>>> print(PyQuery('<div />').show()) <div style="display: block"/> 
PyQuery.siblings(selector=None)
>>> h = '<span><p class="hello">Hi</p><p>Bye</p><img scr=""/></span>'
>>> d = PyQuery(h) >>> d('.hello').siblings() [<p>, <img>] >>> d('.hello').siblings('img') [<img>] 
PyQuery.text(value=<NoDefault>)

Get or set the text representation of sub nodes.

Get the text value:

>>> doc = PyQuery('<div><span>toto</span><span>tata</span></div>') >>> print(doc.text()) toto tata 

Set the text value:

>>> doc.text('Youhou !') [<div>] >>> print(doc) <div>Youhou !</div> 
PyQuery.toggleClass(value)

Alias for?toggle_class()

PyQuery.toggle_class(value)

Toggle a css class to elements

>>> d = PyQuery('<div></div>') >>> d.toggle_class('myclass') [<div.myclass>] >>> d.toggleClass('myclass') [<div>] 
PyQuery.val(value=<NoDefault>)

Set the attribute value:

>>> d = PyQuery('<input />') >>> d.val('Youhou') [<input>] 

Get the attribute value:

>>> d.val()
'Youhou' 
PyQuery.width(value=<NoDefault>)

set/get width of element

PyQuery.wrap(value)

A string of HTML that will be created on the fly and wrapped around each target:

>>> d = PyQuery('<span>youhou</span>') >>> d.wrap('<div></div>') [<div>] >>> print(d) <div><span>youhou</span></div> 
PyQuery.wrapAll(value)

Alias for?wrap_all()

PyQuery.wrap_all(value)

Wrap all the elements in the matched set into a single wrapper element:

>>> d = PyQuery('<div><span>Hey</span><span>you !</span></div>') >>> print(d('span').wrap_all('<div id="wrapper"></div>')) <div id="wrapper"><span>Hey</span><span>you !</span></div> >>> d = PyQuery('<div><span>Hey</span><span>you !</span></div>') >>> print(d('span').wrapAll('<div id="wrapper"></div>')) <div id="wrapper"><span>Hey</span><span>you !</span></div> 
PyQuery.xhtml_to_html()

Remove xhtml namespace:

>>> doc = PyQuery(
... '<html xmlns="http://www.w3.org/1999/xhtml"></html>') >>> doc [<{http://www.w3.org/1999/xhtml}html>] >>> doc.xhtml_to_html() [<html>]


項目中的使用

?

?

?

str(PQ)和PQ.outer_html()都會將部分便簽由<tag></tag>變為<tag />,神奇的是有些標簽變為這樣形式后,
這樣的字符串瀏覽器會解析不出來。'<!DOCTYPE html>'字符串會在變為PQ對象后自動剔除了。

轉載于:https://www.cnblogs.com/Purk/p/5607160.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/457732.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/457732.shtml
英文地址,請注明出處:http://en.pswp.cn/news/457732.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

python配置pip_Python pip源配置

pipy國內鏡像目前有&#xff1a;Win7下配置pip源&#xff1a;1、在win7用戶目錄下創建pip目錄&#xff0c;以用戶user為例&#xff1a;C:\Users\user\pip2、在pip目錄下新建pip.ini文件&#xff1a;C:\Users\user\pip\pip.ini3、配置文件內容&#xff1a;以下是一個簡單的配置示…

Github Pages建立個人博客

使用Github Pages可以建立個人博客。官方教程&#xff1a;https://pages.github.com/步驟&#xff08;以下步驟中假設用戶名為username&#xff09;&#xff1a;1.建立一個項目&#xff0c;項目名為username.github.io2.初始化項目&#xff0c;上傳網頁代碼到github。轉載于:ht…

判斷該網頁是在什么設備打開。

為什么80%的碼農都做不了架構師&#xff1f;>>> <script type"text/javascript"> //判斷訪問終端 var browser{versions:function(){var u navigator.userAgent, app navigator.appVersion;return {trident: u.indexOf(Trident) > -1, //IE內…

python變量和常量_python變量與常量內容:

python變量與常量內容:# 變量&#xff1a;定義世間萬物變化的狀態height 180weight 140age 18tree_name yuyang# print(180)height 180print(height:, height)weight 140print(weight:, weight)age 18print(age:, age)tree_name yuyangprint(tree_name:, tree_name)# 變量的…

EF二級緩存

https://efcache.codeplex.com/ 轉載于:https://www.cnblogs.com/shiningrise/p/5612941.html

python wordpress xmlrpc_python-markdown自動發送wordpress文章(python-xmlrpc-wordpress)

一直熱衷使用Markdown&#xff0c;使用了圖床&#xff0c;以及多款的MD編輯器。wp的后臺太重了&#xff0c;又不想轉 hexo git &#xff0c;對于文章上傳至博客&#xff0c;總想辦法折騰怎么上傳wordprss。之前的解決辦法就是&#xff0c;直接將MD編輯器生成的html復制到wordp…

Android 5.1 - 狀態欄充電標志問題

Android 5.1 Ubuntu14.04 SourceInsigh電量已滿&#xff0c;插著USB頭&#xff0c;觀察Settings - Battery&#xff0c;電量為100%&#xff0c;狀態為full&#xff0c;但仍有充電圖標rust之前有讀過關于StatusBar的代碼。這次直接用SourceInsight找到 StatusBarHeaderView.jav…

kail中tools的安裝和第一個php學習筆記

安裝tools 打開 鼠標右擊選擇 創建文件夾 mkdir cdrom 把tools文件復制到 位置——計算機——cdrom文件夾下 打開 cdrom cd cdrom 復制生成的目錄 解壓文件夾&#xff1a;tar zxvf 粘貼目錄 回車 ls 復制目錄 cd 粘貼目錄 ls 復制后綴為pl的目錄 ./粘貼目錄.d自動下載 Enjoy代表…

面試進階題集錦-持續更新

面向對象的”六原則一法則” - 單一職責原則&#xff1a;一個類只做它該做的事情。&#xff08;單一職責原則想表達的就是”高內聚”&#xff0c;寫代碼最終極的原則只有六個字”高內聚、低耦合”&#xff0c;所謂的高內聚就是一個代碼模塊只完成一項功能&#xff0c;在面向對象…

透明(顏色)漸變背景(顏色透明背景),兼容IE8

filter: progid:DXImageTransform.Microsoft.gradient (GradientType0, startColorstr#00000000, endColorstr#cc000000); -ms-filter: "progid:DXImageTransform.Microsoft.gradient (GradientType0, startColorstr#00000000, endColorstr#cc000000)";一般用filter就…

python pip本地安裝包_python-pip install 安裝包

python-pip install 安裝包國內安裝python包&#xff0c;有時會因為網絡問題&#xff0c;導致package安裝失敗&#xff0c;so,換一種方法解決&#xff1b;pip install package_name;(直接安裝&#xff1b;推薦使用&#xff0c;但因網絡問題&#xff0c;有時安裝失敗)pip instal…

【CDN】域名無法訪問,ping不到,tracert不到

背景&#xff1a;香港服務器&#xff0c;CDN服務商&#xff1a;Incapsula 1、首先猜測&#xff0c;域名是否被墻 原因&#xff1a;ip可以直接訪問到網站&#xff0c;其他域名指向服務器也可訪問 排查&#xff1a;1&#xff09;首先理解&#xff0c;怎樣才算被墻&#xff1a;大陸…

python 庫整理_自己整理的PYTHON庫

1、操作Excel1)Pylightxl地址&#xff1a;https://pypi.org/project/pylightxl文檔&#xff1a;https://pylightxl.readthedocs.io/en/latest/2)Openpyxl地址&#xff1a;https://pypi.org/project/openpyxl/文檔&#xff1a;https://openpyxl.readthedocs.io/en/stable/2、操作…

PHP發送郵件

先看一下HTML如何發送郵件 HTML發送郵件首先得使用超鏈接標簽<a> 之后使用mailto:鏈接收件人郵件地址 用bcc鏈接發件人郵箱&#xff08;加密抄送&#xff0c;cc為直接抄送&#xff09; mailto 和bcc/cc之間用?連接 subject郵件主題&#xff0c;body郵件內容&#xff0…

node.js 實現掃碼二維碼登錄

最近在做一個掃碼登錄功能&#xff0c;為此我還在網上搜了一下關于微信的掃描登錄的實現方式。當這個功能完成了后&#xff0c;我決定將整個實現思路整理出來&#xff0c;方便自己以后查看也方便其他有類似需求的程序猿些。 要實現掃碼登錄我們需要解決兩個問題&#xff1a; 1.…

喇叭正反相位測試音頻_FIR濾波器能給音頻擴聲帶來怎樣的幫助?

隨著數字音頻的快速發展&#xff0c;近些年在音頻擴聲領域&#xff0c;經常能聽到音頻技術人士討論FIR數字濾波器&#xff0c;有些說法和廠家的宣傳難免有些過于神化&#xff0c;有些廠家的技術工程師竟然宣稱&#xff0c;自己的FIR濾波器能把每只揚聲器或者整組擴聲系統的相位…

使用c語言easy—x庫實現實時鐘表

先了解一下easy-x庫 EasyX 是針對 C 的圖形庫&#xff0c;可以幫助 C語言初學者快速上手圖形和游戲編程。 可以通過官網下載&#xff0c;文件很小&#xff0c; easy-x的支持頭文件是 #include<graphics.h>下載之后雙擊打開會有所有easy-x函數的語法和作用&#xff0c;中…

java基礎—方法重載(overload)

一、方法的重載 方法名一樣&#xff0c;但參數不一樣&#xff0c;這就是重載(overload)。 所謂的參數不一樣&#xff0c;主要有兩點&#xff1a;第一是參數的個數不一樣&#xff0c;第二是參數的類型不一樣。只要這兩方面有其中的一方面不一樣就可以構成方法的重載了。 1 packa…

word 編輯域中的漢字_word中插入的cad對象無法雙擊編輯問題解決記錄

昨日&#xff0c;安裝了天正插件5.0后&#xff0c;插入word中的cad圖無法編輯了&#xff0c;彈出提示的大意是檢查是否安裝了cad或者是否關閉了CAD中所有的彈窗。在此之前&#xff0c;計算機裝了office2010和cad2014及cad2018&#xff0c;office自動關聯cad2018&#xff0c;即使…

php實現注冊登陸驗證

歡迎界面很簡單&#xff0c;直接放上代碼 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns"http://www.w3.org/1999/xhtml"> <head&…