<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # 4.3 使用pyquery [官網文檔](http://pyquery.readthedocs.io/en/latest/api.html) ### 1. 初始化 PyQuery 初始化的時候也需要傳入 HTML 數據源來初始化一個操作對象,它的初始化方式有多種,比如直接傳入字符串,傳入 URL,傳文件名 #### 字符串初始化 {#字符串初始化} ```text html = ''' <div> <ul> <li class="item-0">first item</li> <li class="item-1"><a href="link2.html">second item</a></li> <li class="item-0 active"><a href="link3.html"><span class="bold">third item</span></a></li> <li class="item-1 active"><a href="link4.html">fourth item</a></li> <li class="item-0"><a href="link5.html">fifth item</a></li> </ul> </div> ''' from pyquery import PyQuery as pq doc = pq(html) print(doc('li')) ``` 運行結果: ```text <li class="item-0">first item</li> <li class="item-1"><a href="link2.html">second item</a></li> <li class="item-0 active"><a href="link3.html"><span class="bold">third item</span></a></li> <li class="item-1 active"><a href="link4.html">fourth item</a></li> <li class="item-0"><a href="link5.html">fifth item</a></li> ``` #### URL初始化 {#url初始化} 傳入指定參數為 url ```text from pyquery import PyQuery as pq doc = pq(url='https://github.com/CoderAngle') print(doc('title')) ``` 運行結果: ```text <title>CoderAngle · GitHub</title> ``` 等同于下面這個: 使用requests模塊訪問url然后返回源碼 ```text from pyquery import PyQuery as pq import requests doc = pq(requests.get('https://github.com/CoderAngle').text) print(doc('title')) ``` #### 文件初始化 {#文件初始化} 傳入參數指定為 filename ```text from pyquery import PyQuery as pq doc = pq(filename='test.html') print(doc('li')) ``` ### 2. 基本CSS選擇器 {#3-基本css選擇器} 實例: ```text html = ''' <div id="container"> <ul class="list"> <li class="item-0">first item</li> <li class="item-1"><a href="link2.html">second item</a></li> <li class="item-0 active"><a href="link3.html"><span class="bold">third item</span></a></li> <li class="item-1 active"><a href="link4.html">fourth item</a></li> <li class="item-0"><a href="link5.html">fifth item</a></li> </ul> </div> ''' from pyquery import PyQuery as pq doc = pq(html) print(doc('#container .list li')) print(type(doc('#container .list li'))) ``` 運行結果: ```text <li class="item-0">first item</li> <li class="item-1"><a href="link2.html">second item</a></li> <li class="item-0 active"><a href="link3.html"><span class="bold">third item</span></a></li> <li class="item-1 active"><a href="link4.html">fourth item</a></li> <li class="item-0"><a href="link5.html">fifth item</a></li> <class 'pyquery.pyquery.PyQuery'> ``` ### 3. 查找節點 {#4-查找節點} pyquery的常用的查詢函數,這些函數和 jQuery 中的函數用法完全相同 #### 子節點 {#子節點} 查找子節點需要用到 find\(\) 方法,傳入的參數是 CSS 選擇器 ```text from pyquery import PyQuery as pq doc = pq(html) items = doc('.list') print(type(items)) print(items) list_itmes = items.find('li') print(type(list_itmes)) print(list_itmes) ``` 運行結果: ```text <class 'pyquery.pyquery.PyQuery'> <ul class="list"> <li class="item-0">first item</li> <li class="item-1"><a href="link2.html">second item</a></li> <li class="item-0 active"><a href="link3.html"><span class="bold">third item</span></a></li> <li class="item-1 active"><a href="link4.html">fourth item</a></li> <li class="item-0"><a href="link5.html">fifth item</a></li> </ul> <class 'pyquery.pyquery.PyQuery'> <li class="item-0">first item</li> <li class="item-1"><a href="link2.html">second item</a></li> <li class="item-0 active"><a href="link3.html"><span class="bold">third item</span></a></li> <li class="item-1 active"><a href="link4.html">fourth item</a></li> <li class="item-0"><a href="link5.html">fifth item</a></li>() ``` #### children\(\) find\(\) 的查找范圍是節點的所有子孫節點,而如果我們只想查找子節點,那可以用 children\(\) 方法 ```text from pyquery import PyQuery as pq doc = pq(html) lis = doc.children() print(type(lis)) print(lis) ``` 運行結果: ```text <class 'pyquery.pyquery.PyQuery'> <ul class="list"> <li class="item-0">first item</li> <li class="item-1"><a href="link2.html">second item</a></li> <li class="item-0 active"><a href="link3.html"><span class="bold">third item</span></a></li> <li class="item-1 active"><a href="link4.html">fourth item</a></li> <li class="item-0"><a href="link5.html">fifth item</a></li> </ul> ``` 實例:篩選出子節點class屬性值為active ```text from pyquery import PyQuery as pq items = pq(html) lis = items.children('.list .active') print(type(lis)) print(lis) ``` 運行結果: ```text <class 'pyquery.pyquery.PyQuery'> <li class="item-0 active"><a href="link3.html"><span class="bold">third item</span></a></li> <li class="item-1 active"><a href="link4.html">fourth item</a></li> ``` #### 父節點 {#父節點} 可以用 parent\(\) 方法來獲取某個節點的父節點 實例:返回class屬性值list當前節點的父節點下的內容 ```text html = ''' <div class="wrap"> <div id="container"> <ul class="list"> <li class="item-0">first item</li> <li class="item-1"><a href="link2.html">second item</a></li> <li class="item-0 active"><a href="link3.html"><span class="bold">third item</span></a></li> <li class="item-1 active"><a href="link4.html">fourth item</a></li> <li class="item-0"><a href="link5.html">fifth item</a></li> </ul> </div> </div> ''' from pyquery import PyQuery as pq doc = pq(html) items = doc('.list') container = items.parent() print(type(container)) print(container) ``` 運行結果: ```text <class 'pyquery.pyquery.PyQuery'> <div id="container"> <ul class="list"> <li class="item-0">first item</li> <li class="item-1"><a href="link2.html">second item</a></li> <li class="item-0 active"><a href="link3.html"><span class="bold">third item</span></a></li> <li class="item-1 active"><a href="link4.html">fourth item</a></li> <li class="item-0"><a href="link5.html">fifth item</a></li> </ul> </div> ``` #### parents\(\) parents\(\) 方法會返回所有的祖先節點 ```text from pyquery import PyQuery as pq doc = pq(html) items = doc('.list') parents= items.parents() print(type(parents)) print(parents) ``` 結果會有兩個節點 運行結果: ```text <class 'pyquery.pyquery.PyQuery'> <div class="wrap"> <div id="container"> <ul class="list"> <li class="item-0">first item</li> <li class="item-1"><a href="link2.html">second item</a></li> <li class="item-0 active"><a href="link3.html"><span class="bold">third item</span></a></li> <li class="item-1 active"><a href="link4.html">fourth item</a></li> <li class="item-0"><a href="link5.html">fifth item</a></li> </ul> </div> </div><div id="container"> <ul class="list"> <li class="item-0">first item</li> <li class="item-1"><a href="link2.html">second item</a></li> <li class="item-0 active"><a href="link3.html"><span class="bold">third item</span></a></li> <li class="item-1 active"><a href="link4.html">fourth item</a></li> <li class="item-0"><a href="link5.html">fifth item</a></li> </ul> </div> ``` 篩選出指定父節點 實例:篩選出屬性值為wrap的父節點 ```text from pyquery import PyQuery as pq doc = pq(html) items = doc('.list') parents = items.parents('.wrap') print(type(parents)) print(parents) ``` 運行結果: ```text <class 'pyquery.pyquery.PyQuery'> <div class="wrap"> <div id="container"> <ul class="list"> <li class="item-0">first item</li> <li class="item-1"><a href="link2.html">second item</a></li> <li class="item-0 active"><a href="link3.html"><span class="bold">third item</span></a></li> <li class="item-1 active"><a href="link4.html">fourth item</a></li> <li class="item-0"><a href="link5.html">fifth item</a></li> </ul> </div> </div> ``` #### 兄弟節點 {#兄弟節點} 要獲取兄弟節點可以使用 siblings\(\) 方法 ```text from pyquery import PyQuery as pq doc = pq(html) li = doc('.list .item-0.active') print(li.siblings()) ``` 運行結果: ```text <li class="item-1"><a href="link2.html">second item</a></li> <li class="item-0">first item</li> <li class="item-1 active"><a href="link4.html">fourth item</a></li> <li class="item-0"><a href="link5.html">fifth item</a></li> ``` 篩選某個指定兄弟節點 ```text from pyquery import PyQuery as pq doc = pq(html) li = doc('.list .item-0.active') print(li.siblings('.active')) ``` 運行結果: ```text <li class="item-1 active"><a href="link4.html">fourth item</a></li> ``` ### 4. 遍歷 {#5-遍歷} 對于多個節點的結果,需要遍歷來獲取了 實例:把每一個 li 節點進行遍歷,,需要調用 items\(\) 方法 ```text from pyquery import PyQuery as pq doc = pq(html) lis = doc('li').items() print(type(lis)) for li in lis: print(li,type(li)) ``` 運行結果: ```text <class 'generator'> <li class="item-0">first item</li> <class 'pyquery.pyquery.PyQuery'> <li class="item-1"><a href="link2.html">second item</a></li> <class 'pyquery.pyquery.PyQuery'> <li class="item-0 active"><a href="link3.html"><span class="bold">third item</span></a></li> <class 'pyquery.pyquery.PyQuery'> <li class="item-1 active"><a href="link4.html">fourth item</a></li> <class 'pyquery.pyquery.PyQuery'> <li class="item-0"><a href="link5.html">fifth item</a></li> <class 'pyquery.pyquery.PyQuery'> ``` ### 5.獲取信息 {#6-獲取信息} * 獲取屬性 * 獲取文本 #### 獲取屬性 {#獲取屬性} 使用attr\(\) 方法獲取屬性 ```text html = ''' <div class="wrap"> <div id="container"> <ul class="list"> <li class="item-0">first item</li> <li class="item-1"><a href="link2.html">second item</a></li> <li class="item-0 active"><a href="link3.html"><span class="bold">third item</span></a></li> <li class="item-1 active"><a href="link4.html">fourth item</a></li> <li class="item-0"><a href="link5.html">fifth item</a></li> </ul> </div> </div> ''' from pyquery import PyQuery as pq doc = pq(html) a = doc('.item-0.active a') print(a,type(a)) print(a.attr('href')) print(a.attr.href) ``` 有兩種獲取屬性方法: ```text a.attr('href') a.attr.href ``` 運行結果: ```text <a href="link3.html"><span class="bold">third item</span></a> <class 'pyquery.pyquery.PyQuery'> link3.html link3.html ``` 當返回結果包含多個節點時,調用 attr\(\) 方法只會得到第一個節點的屬性 ```text from pyquery import PyQuery as pq doc = pq(html) a = doc('a') for item in a.items(): print(item.attr('href')) ``` 運行結果: ```text link2.html link3.html link4.html link5.html ``` #### 獲取文本 {#獲取文本} 調用了 text\(\) 方法,就可以獲取其內部的文本信息了,它會忽略掉節點內部包含的所有 HTML,只返回純文字內容 ```text html = ''' <div class="wrap"> <div id="container"> <ul class="list"> <li class="item-0">first item</li> <li class="item-1"><a href="link2.html">second item</a></li> <li class="item-0 active"><a href="link3.html"><span class="bold">third item</span></a></li> <li class="item-1 active"><a href="link4.html">fourth item</a></li> <li class="item-0"><a href="link5.html">fifth item</a></li> </ul> </div> </div> ''' from pyquery import PyQuery as pq doc = pq(html) a = doc('.item-0.active a') print(a) print(a.text()) ``` 運行結果: ```text <a href="link3.html"><span class="bold">third item</span></a> third item ``` 獲取這個節點內部的 HTML 文本,就可以用 html\(\) 方法 ```text from pyquery import PyQuery as pq doc = pq(html) a = doc('.item-0.active a') print(a) print(a.html()) ``` 運行結果: ```text <a href="link3.html"><span class="bold">third item</span></a> <span class="bold">third item</span> ``` 在多個節點的情況下,html\(\) 方法返回第一個 li 節點的內部 HTML 文本,而 text\(\) 返回所有的 li 節點內部純文本 ### 6. 節點操作 {#7-節點操作} PyQuery 提供了一系列方法來對節點進行動態修改操作 #### add\_class、remove\_class {#addclass、removeclass} 添加、刪除類屬性 ```text from pyquery import PyQuery as pq doc = pq(html) li = doc('.item-0.active') print(li) li.remove_class('active') print(li) li.add_class("active") print(li) ``` 返回結果: ```text <li class="item-0 active"><a href="link3.html"><span class="bold">third item</span></a></li> <li class="item-0"><a href="link3.html"><span class="bold">third item</span></a></li> <li class="item-0 active"><a href="link3.html"><span class="bold">third item</span></a></li> ``` #### attr、text、html {#attr、text、html} ```text from pyquery import PyQuery as pq doc = pq(html) li = doc('.item-0.active') print(li) li.attr('name','link') print(li) li.text('chaned item') print(li) li.html('<span>changed item</span') print(li) ``` attr\(\) 方法如果只傳入第一個參數屬性名,則是獲取這個屬性值,如果傳入第二個參數,可以用來修改屬性值,text\(\) 和 html\(\) 方法如果不傳參數是獲取節點內純文本和 HTML 文本,如果傳入參數則是進行賦值 返回結果: ```text <li class="item-0 active"><a href="link3.html"><span class="bold">third item</span></a></li> <li class="item-0 active" name="link"><a href="link3.html"><span class="bold">third item</span></a></li> <li class="item-0 active" name="link">chaned item</li> <li class="item-0 active" name="link"><span>changed item</span></li> ``` #### remove {#remove} 移除 實例: ```text html = ''' <div class="wrap"> Hello, World <p>This is a paragraph.</p> </div> ''' from pyquery import PyQuery as pq doc = pq(html) wrap = doc('.wrap') print(wrap.text()) ``` 運行結果: ```text Hello, World This is a paragraph. ``` 例子:移除p節點 ```text from pyquery import PyQuery as pq doc = pq(html) wrap = doc('.wrap') wrap.find('p').remove() print(wrap.text()) ``` 運行結果: ```text Hello, World ``` 另外其實還有很多節點操作的方法,比如 append\(\)、empty\(\)、prepend\(\) 等方法,這些函數和 jQuery 的用法是完全一致的,詳細的用法可以參考官方文檔: [http://pyquery.readthedocs.io/en/latest/api.html](http://pyquery.readthedocs.io/en/latest/api.html) ### 7.偽類選擇器 {#8-偽類選擇器} CSS選擇器用法:[http://www.w3school.com.cn/css/index.asp](http://www.w3school.com.cn/css/index.asp) 例如選擇第一個節點、最后一個節點、奇偶數節點、包含某一文本的節點等等 實例: ```text html = ''' <div class="wrap"> <div id="container"> <ul class="list"> <li class="item-0">first item</li> <li class="item-1"><a href="link2.html">second item</a></li> <li class="item-0 active"><a href="link3.html"><span class="bold">third item</span></a></li> <li class="item-1 active"><a href="link4.html">fourth item</a></li> <li class="item-0"><a href="link5.html">fifth item</a></li> </ul> </div> </div> ''' from pyquery import PyQuery as pq doc = pq(html) # 第一個 li 節點 li = doc('li:first-child') print(li) # 最后一個 li 節點 li = doc('li:last-child') print(li) # 第二個 li 節點 li = doc('li:nth-child(2)') print(li) # 第三個 li 之后的 li 節點 li = doc('li:gt(2)') print(li) # 偶數位置的 li 節點 li = doc('li:nth-child(2n)') print(li) # 包含 second 文本的 li 節點 li = doc('li:contains(second)') print(li) ``` 運行結果: ```text <li class="item-0">first item</li> <li class="item-0"><a href="link5.html">fifth item</a></li> <li class="item-1"><a href="link2.html">second item</a></li> <li class="item-1 active"><a href="link4.html">fourth item</a></li> <li class="item-0"><a href="link5.html">fifth item</a></li> <li class="item-1"><a href="link2.html">second item</a></li> <li class="item-1 active"><a href="link4.html">fourth item</a></li> <li class="item-1"><a href="link2.html">second item</a></li> ``` ### 8.細節 更多的內容,參考官方文檔:[http://pyquery.readthedocs.io](http://pyquery.readthedocs.io/) ## CSS3 選擇器 參考文檔:[http://www.w3school.com.cn/cssref/css\_selectors.asp](http://www.w3school.com.cn/cssref/css_selectors.asp) | 選擇器 | 例子 | 例子描述 | | :--- | :--- | :--- | | .class | .intro | 選擇 class="intro" 的所有元素。 | | \#id | \#firstname | 選擇 id="firstname" 的所有元素。 | | \* | \* | 選擇所有元素。 | | element | p | 選擇所有 &lt;p&gt; 元素。 | | element,element | div,p | 選擇所有 &lt;div&gt; 元素和所有 &lt;p&gt; 元素。 | | elementelement | div p | 選擇 &lt;div&gt; 元素內部的所有 &lt;p&gt; 元素。 | | element&gt;element | div&gt;p | 選擇父元素為 &lt;div&gt; 元素的所有 &lt;p&gt; 元素。 | | element+element | div+p | 選擇緊接在 &lt;div&gt; 元素之后的所有 &lt;p&gt; 元素。 | | \[attribute\] | \[target\] | 選擇帶有 target 屬性所有元素。 | | \[attribute=value\] | \[target=\_blank\] | 選擇 target="\_blank" 的所有元素。 | | \[attribute~=value\] | \[title~=flower\] | 選擇 title 屬性包含單詞 "flower" 的所有元素。 | | \[attribute\|=value\] | \[lang\|=en\] | 選擇 lang 屬性值以 "en" 開頭的所有元素。 | | :link | a:link | 選擇所有未被訪問的鏈接。 | | :visited | a:visited | 選擇所有已被訪問的鏈接。 | | :active | a:active | 選擇活動鏈接。 | | :hover | a:hover | 選擇鼠標指針位于其上的鏈接。 | | :focus | input:focus | 選擇獲得焦點的 input 元素。 | | :first-letter | p:first-letter | 選擇每個 &lt;p&gt; 元素的首字母。 | | :first-line | p:first-line | 選擇每個 &lt;p&gt; 元素的首行。 | | :first-child | p:first-child | 選擇屬于父元素的第一個子元素的每個 &lt;p&gt; 元素。 | | :before | p:before | 在每個 &lt;p&gt; 元素的內容之前插入內容。 | | :after | p:after | 在每個 &lt;p&gt; 元素的內容之后插入內容。 | | :lang\(language\) | p:lang\(it\) | 選擇帶有以 "it" 開頭的 lang 屬性值的每個 &lt;p&gt; 元素。 | | element1~element2 | p~ul | 選擇前面有 &lt;p&gt; 元素的每個 &lt;ul&gt; 元素。 | | \[attribute^=value\] | a\[src^="https"\] | 選擇其 src 屬性值以 "https" 開頭的每個 &lt;a&gt; 元素。 | | \[attribute$=value\] | a\[src$=".pdf"\] | 選擇其 src 屬性以 ".pdf" 結尾的所有 &lt;a&gt; 元素。 | | \[attribute\*=value\] | a\[src\*="abc"\] | 選擇其 src 屬性中包含 "abc" 子串的每個 &lt;a&gt; 元素。 | | :first-of-type | p:first-of-type | 選擇屬于其父元素的首個 &lt;p&gt; 元素的每個 &lt;p&gt; 元素。 | | :last-of-type | p:last-of-type | 選擇屬于其父元素的最后 &lt;p&gt; 元素的每個 &lt;p&gt; 元素。 | | :only-of-type | p:only-of-type | 選擇屬于其父元素唯一的 &lt;p&gt; 元素的每個 &lt;p&gt; 元素。 | | :only-child | p:only-child | 選擇屬于其父元素的唯一子元素的每個 &lt;p&gt; 元素。 | | :nth-child\(n\) | p:nth-child\(2\) | 選擇屬于其父元素的第二個子元素的每個 &lt;p&gt; 元素。 | | :nth-last-child\(n\) | p:nth-last-child\(2\) | 同上,從最后一個子元素開始計數。 | | :nth-of-type\(n\) | p:nth-of-type\(2\) | 選擇屬于其父元素第二個 &lt;p&gt; 元素的每個 &lt;p&gt; 元素。 | | :nth-last-of-type\(n\) | p:nth-last-of-type\(2\) | 同上,但是從最后一個子元素開始計數。 | | :last-child | p:last-child | 選擇屬于其父元素最后一個子元素每個 &lt;p&gt; 元素。 | | :root | :root | 選擇文檔的根元素。 | | :empty | p:empty | 選擇沒有子元素的每個 &lt;p&gt; 元素(包括文本節點)。 | | :target | \#news:target | 選擇當前活動的 \#news 元素。 | | :enabled | input:enabled | 選擇每個啟用的 &lt;input&gt; 元素。 | | :disabled | input:disabled | 選擇每個禁用的 &lt;input&gt; 元素 | | :checked | input:checked | 選擇每個被選中的 &lt;input&gt; 元素。 | | :not\(selector\) | :not\(p\) | 選擇非 &lt;p&gt; 元素的每個元素。 | | ::selection | ::selection | 選擇被用戶選取的元素部分。 |
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看