[TOC]
## PyQuery庫的介紹
作為一個略懂JQuery的我,在眾多的HTML解析庫中,我只選擇PyQuery庫,因為它的元素定位與JQuery幾乎完全相同,懂得了JQuery后,不需再去學習一些奇奇怪怪的語法了。
用JQuery的元素定位方式,既強大又靈活。
>[info] pyquery: a jquery-like library for python
官網地址:[http://pyquery.readthedocs.io/en/latest/](http://pyquery.readthedocs.io/en/latest/)
## 安裝PyQuery
```cmd
pip install pyquery
```
## PyQuery 初始化
初始化的時候一般有三種傳入方式:傳入字符串,傳入url,傳入文件
舉個簡單例子如
```python
from pyquery import PyQuery as pq
d1 = pq("<html></html>")
d2 = pq(url="http://www.hmoore.net/@guanfuchang")
d3 = pq(filename="report.html")
print(type(d1))
```
打印初始化d1的類型
```cmd
<class 'pyquery.pyquery.PyQuery'>
```
可知,這里的d1是一個pyquery對象。
## PyQuery 元素定位
```cmd
>>> from pyquery import PyQuery as pq
```
### 根據class定位
獲取用戶昵稱

```cmd
>>> d = pq(url="http://www.hmoore.net/@guanfuchang")
>>> d('.ui.header.center.aligned').text()
'Milton'
>>> d('.aligned').text()
'Milton'
```
### 根據id定位
獲取用戶昵稱

```cmd
>>> d=pq(url="http://www.cnblogs.com/guanfuchang")
>>> d('#Header1_HeaderTitle').text()
'鯊魚逛大街'
```
### 根據標簽定位
獲取用戶頭像鏈接

```cmd
>>> d=pq(url="http://www.hmoore.net/@guanfuchang")
>>>
>>> d('.ui.container img').attr('src')
'https://avatar.kancloud.cn/05/97f2c4fac2554876aa49a68fc931b4!large'
>>>
```
### 根據屬性定位
根據a標簽的鏈接屬性獲取標題

```cmd
>>> d=pq(url="http://www.hmoore.net/@guanfuchang")
>>> d('a[href="/@guanfuchang?tab=attention"]').text()
'關注'
>>>
```
### 根據多層級結合定位
獲取第一本書的標題

```cmd
>>> d=pq(url="http://www.hmoore.net/@guanfuchang")
>>> d('.items .header').text()
'輕松掌握接口測試_工具篇 python 快速入門'
>>>
>>> d('.items .header:first').text()
'輕松掌握接口測試_工具篇 python 快速入門'
>>>
```
如果要獲取第二本書的標題,要如何處理?
```cmd
>>> d('.items .header').eq(1).text()
'python 快速入門'
```
更多定位方法,可以參照jquery的選擇器
參考文檔 [http://www.w3school.com.cn/jquery/jquery_ref_selectors.asp](http://www.w3school.com.cn/jquery/jquery_ref_selectors.asp)
## PyQuery 對象的屬性與方法
>[warning] 重復強調 **最好的學習文檔是源代碼**,請認真閱讀PyQuery的源碼,通過源碼才是快速掌握一個庫的正確姿勢。
* items(self, selector=None): 迭代元素,返回PyQuery對象
```cmd
>>> d = PyQuery('<div><span>foo</span><span>bar</span></div>')
>>>
>>> d('span')
[<span>, <span>]
>>>
>>> type(d('span'))
<class 'pyquery.pyquery.PyQuery'>
>>>
>>> type(d('span').items())
<class 'generator'>
>>>
>>> [i.text() for i in d('span').items()]
['foo', 'bar']
>>>
>>> [i.text() for i in d.items('span')]
['foo', 'bar']
```
* html(self, value=no_default, \**kwargs): 獲取或設置子節點的HTML源碼
```cmd
>>> d = PyQuery('<div><span>foo</span><span>bar</span></div>')
>>>
>>> d.html()
'<span>foo</span><span>bar</span>'
```
* text(self, value=no_default): 獲取或設置子節點的文本內容
```cmd
>>> d = PyQuery('<div><span>foo</span><span>bar</span></div>')
>>>
>>> d.text()
'foo bar'
>>>
```
* prev(self, selector=None): 獲取同級節點中的前一個節點
* next(self, selector=None): 獲取同級節點中的后一個節點
```cmd
>>> d = PyQuery('<div class="zero"><span class="first">foo</span><span class="two">bar</span></div>')
>>>
>>> d('.two').prev()
[<span.first>]
>>>
>>> d('.first').prev()
[]
>>> d('.first').next()
[<span.two>]
```
* siblings(self, selector=None): 獲取同級節點的所有兄弟節點或指定兄弟節點
```cmd
>>> d = PyQuery('<div class="zero"><span class="first">foo</span><span class="two">bar</span><img src=""/></div>')
>>>
>>> d('.two').siblings()
[<span.first>, <img>]
>>>
>>> d('.two').siblings('img')
[<img>]
```
* parents(self, selector=None):獲取當前節點的所有祖先節點
* parent(): 獲取當前節點的父節點
```cmd
>>> d = PyQuery('<div class="root"><div class="zero"><span class="first">foo</span><span class="two">bar</span><img src=""/></div></div>')
>>>
>>> d('.two').parents()
[<div.root>, <div.zero>]
>>>
>>> d('.two').parent()
[<div.zero>]
```
* children(self, selector=None): 獲取所有子節點或指定的子節點
```cmd
>>> d = PyQuery('<div class="root"><div class="zero"><span class="first">foo</span><span class="two">bar</span><img src=""/></div></div>')
>>>
>>> d('.zero').children()
[<span.first>, <span.two>, <img>]
>>>
>>> d('.zero').children('.two')
[<span.two>]
```
* find(self, selector): 在當前節點的子節點中尋找指定節點
```cmd
>>> d = PyQuery('<div class="root"><div class="zero"><span class="first">foo</span><span class="two">bar</span><img src=""/></div></div>')
>>>
>>> d.find('span')
[<span.first>, <span.two>]
>>>
>>> d.find('.zero').find('.two')
[<span.two>]
```
* filter(self, selector): 在當前節點集之中過濾出符合條件的節點
```cmd
>>> d = PyQuery('<div class="root"><div class="zero"><span class="first">foo</span><span class="two">bar</span><img src=""/></div></div>')
>>>
>>> d('span').filter('.two')
[<span.two>]
>>>
```
* eq(self, index): 根據索引返回PyQuery
```cmd
>>> d = PyQuery('<div class="root"><div class="zero"><span class="first">foo</span><span class="two">bar</span><img src=""/></div></div>')
>>>
>>> d.find('span')
[<span.first>, <span.two>]
>>>
>>> d.find('span').eq(0)
[<span.first>]
>>>
>>> d.find('span').eq(1)
[<span.two>]
```
* size(self): 返回所定位節點數量
```cmd
>>> d = PyQuery('<div class="root"><div class="zero"><span class="first">foo</span><span class="two">bar</span><img src=""/></div></div>')
>>>
>>> d.find('span')
[<span.first>, <span.two>]
>>>
>>> d.find('span').size()
2
```
* attr(self, *args, \**kwargs): 元素屬性操作
```cmd
>>> d = PyQuery('<div class="root"><div class="zero"><span class="first">foo</span><span class="two">bar</span><img src="http://abc.jpb"/></div></div>')
>>>
>>> d('img').attr('src')
'http://abc.jpb'
>>>
>>> d('img').attr('k1','v1')
[<img>]
>>>
>>> d('img').__html__()
'<img src="http://abc.jpb" k1="v1">'
>>>
>>> d('img').attr(**{'k1':'v1','k2':'v2'})
[<img>]
>>>
>>> d('img').__html__()
'<img src="http://abc.jpb" k1="v1" k2="v2">'
>>>
```
* 還有很多方法是對元素的屬性,css,dom進行修改的,這些我暫時用得不多,用得比較多的是定位元素,獲取元素的內容與屬性。
<hr style="margin-top:100px">
:-: 
***微信掃一掃,關注“python測試開發圈”,了解更多測試教程!***
- 前言
- chapter01_開發環境
- chapter02_字符串的使用
- chapter03_列表的使用
- chapter04_字典的使用
- chapter05_數字的使用
- chapter06_元組的使用
- chapter07_集合的使用
- chapter08_輸入輸出
- chapter09_控制流程
- chapter10_實例練習_登錄1
- chapter11_python函數入門
- chapter12_python中的類
- chapter13_輕松玩轉python中的模塊管理
- chapter14_掌握學習新模塊的技巧
- chapter15_通過os模塊與操作系統交互
- chapter16_子進程相關模塊(subprocess)
- chapter17_時間相關模塊(time & datetime)
- chapter18_序列化模塊(json)
- chapter19_加密模塊(hashlib)
- chapter20_文件的讀與寫
- chapter21_階段考核2_登錄
- chapter22_小小算法挑戰(排序&二分法)
- chapter23_用多線程來搞事!
- chapter24_HTTP接口請求(requests)
- chapter25_接口測試框架(pytest)
- chapter26_階段考核3_HTTP接口測試
- chapter27_HTML解析(pyquery)
- chapter28_階段考核4_爬蟲下載網易汽車
- chapter29_python中的那些編碼坑
- chapter30_MySQL數據庫操作
- chapter31 高級特性_迭代器與生成器
- chapter32 高級特性_裝飾器
- chapter33 高級特性_列表處理