# 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 | 選擇所有 <p> 元素。 |
| element,element | div,p | 選擇所有 <div> 元素和所有 <p> 元素。 |
| elementelement | div p | 選擇 <div> 元素內部的所有 <p> 元素。 |
| element>element | div>p | 選擇父元素為 <div> 元素的所有 <p> 元素。 |
| element+element | div+p | 選擇緊接在 <div> 元素之后的所有 <p> 元素。 |
| \[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 | 選擇每個 <p> 元素的首字母。 |
| :first-line | p:first-line | 選擇每個 <p> 元素的首行。 |
| :first-child | p:first-child | 選擇屬于父元素的第一個子元素的每個 <p> 元素。 |
| :before | p:before | 在每個 <p> 元素的內容之前插入內容。 |
| :after | p:after | 在每個 <p> 元素的內容之后插入內容。 |
| :lang\(language\) | p:lang\(it\) | 選擇帶有以 "it" 開頭的 lang 屬性值的每個 <p> 元素。 |
| element1~element2 | p~ul | 選擇前面有 <p> 元素的每個 <ul> 元素。 |
| \[attribute^=value\] | a\[src^="https"\] | 選擇其 src 屬性值以 "https" 開頭的每個 <a> 元素。 |
| \[attribute$=value\] | a\[src$=".pdf"\] | 選擇其 src 屬性以 ".pdf" 結尾的所有 <a> 元素。 |
| \[attribute\*=value\] | a\[src\*="abc"\] | 選擇其 src 屬性中包含 "abc" 子串的每個 <a> 元素。 |
| :first-of-type | p:first-of-type | 選擇屬于其父元素的首個 <p> 元素的每個 <p> 元素。 |
| :last-of-type | p:last-of-type | 選擇屬于其父元素的最后 <p> 元素的每個 <p> 元素。 |
| :only-of-type | p:only-of-type | 選擇屬于其父元素唯一的 <p> 元素的每個 <p> 元素。 |
| :only-child | p:only-child | 選擇屬于其父元素的唯一子元素的每個 <p> 元素。 |
| :nth-child\(n\) | p:nth-child\(2\) | 選擇屬于其父元素的第二個子元素的每個 <p> 元素。 |
| :nth-last-child\(n\) | p:nth-last-child\(2\) | 同上,從最后一個子元素開始計數。 |
| :nth-of-type\(n\) | p:nth-of-type\(2\) | 選擇屬于其父元素第二個 <p> 元素的每個 <p> 元素。 |
| :nth-last-of-type\(n\) | p:nth-last-of-type\(2\) | 同上,但是從最后一個子元素開始計數。 |
| :last-child | p:last-child | 選擇屬于其父元素最后一個子元素每個 <p> 元素。 |
| :root | :root | 選擇文檔的根元素。 |
| :empty | p:empty | 選擇沒有子元素的每個 <p> 元素(包括文本節點)。 |
| :target | \#news:target | 選擇當前活動的 \#news 元素。 |
| :enabled | input:enabled | 選擇每個啟用的 <input> 元素。 |
| :disabled | input:disabled | 選擇每個禁用的 <input> 元素 |
| :checked | input:checked | 選擇每個被選中的 <input> 元素。 |
| :not\(selector\) | :not\(p\) | 選擇非 <p> 元素的每個元素。 |
| ::selection | ::selection | 選擇被用戶選取的元素部分。 |
- 介紹
- 1.開發環境配置
- 1.1 python3的安裝
- 1.1.1 windows下的安裝
- 1.1.2 Linux下的安裝
- 1.1.3 Mac下的安裝
- 1.2 請求庫的安裝
- 1.2.1 requests的安裝
- 1.2.2 selenium的安裝
- 1.2.3 ChromeDriver的安裝
- 1.2.4 GeckoDriver 的安裝
- 1.2.5 PhantomJS的安裝
- 1.2.6 aiohttp的安裝
- 1.3 解析庫的安裝
- 1.3.1 lxml的安裝
- 1.3.2 Beautiful Soup的安裝
- 1.3.3 pyquery的安裝
- 1.3.4 tesserocr的安裝
- 1.4 數據庫的安裝
- 1.4.1 MySQL的安裝
- 1.4.2 MongoDB的安裝
- 1.4.3 Redis的安裝
- 1.5 存儲庫的安裝
- 1.5.1 PyMySQL的安裝
- 1.5.2 PyMongo的安裝
- 1.5.3 redis-py的安裝
- 1.5.4 RedisDump的安裝
- 1.6 Web庫的安裝
- 1.6.1 Flask的安裝
- 1.6.2 Tornado的安裝
- 1.7 App爬取相關庫的安裝
- 1.7.1 Charles的安裝
- 1.7.2 mitmproxy的安裝
- 1.7.3 Appium的安裝
- 1.8 爬蟲框架的安裝
- 1.8.1 pyspider的安裝
- 1.8.2 Scrapy的安裝
- 1.8.3 Scrapy-Splash的安裝
- 1.8.4 ScrapyRedis的安裝
- 1.9 布署相關庫的安裝
- 1.9.1 Docker的安裝
- 1.9.2 Scrapyd的安裝
- 1.9.3 ScrapydClient的安裝
- 1.9.4 ScrapydAPI的安裝
- 1.9.5 Scrapyrt的安裝
- 1.9.6-Gerapy的安裝
- 2.爬蟲基礎
- 2.1 HTTP 基本原理
- 2.1.1 URI和URL
- 2.1.2 超文本
- 2.1.3 HTTP和HTTPS
- 2.1.4 HTTP請求過程
- 2.1.5 請求
- 2.1.6 響應
- 2.2 網頁基礎
- 2.2.1網頁的組成
- 2.2.2 網頁的結構
- 2.2.3 節點樹及節點間的關系
- 2.2.4 選擇器
- 2.3 爬蟲的基本原理
- 2.3.1 爬蟲概述
- 2.3.2 能抓怎樣的數據
- 2.3.3 javascript渲染的頁面
- 2.4 會話和Cookies
- 2.4.1 靜態網頁和動態網頁
- 2.4.2 無狀態HTTP
- 2.4.3 常見誤區
- 2.5 代理的基本原理
- 2.5.1 基本原理
- 2.5.2 代理的作用
- 2.5.3 爬蟲代理
- 2.5.4 代理分類
- 2.5.5 常見代理設置
- 3.基本庫使用
- 3.1 使用urllib
- 3.1.1 發送請求
- 3.1.2 處理異常
- 3.1.3 解析鏈接
- 3.1.4 分析Robots協議
- 3.2 使用requests
- 3.2.1 基本用法
- 3.2.2 高級用法
- 3.3 正則表達式
- 3.4 抓取貓眼電影排行
- 4.解析庫的使用
- 4.1 使用xpath
- 4.2 使用Beautiful Soup
- 4.3 使用pyquery
- 5.數據存儲
- 5.1 文件存儲
- 5.1.1 TXT 文件存儲
- 5.1.2 JSON文件存儲
- 5.1.3 CSV文件存儲
- 5.2 關系型數據庫存儲
- 5.2.1 MySQL的存儲
- 5.3 非關系數據庫存儲
- 5.3.1 MongoDB存儲
- 5.3.2 Redis存儲
- 6.Ajax數據爬取
- 6.1 什么是Ajax
- 6.2 Ajax分析方法
- 6.3 Ajax結果提取
- 6.4 分析Ajax爬取今日頭條街拍美圖
- 7.動態渲染頁面爬取
- 7.1 Selenium的使用
- 7.2 Splash的使用
- 7.3 Splash負載均衡配置
- 7.4 使用selenium爬取淘寶商品
- 8.驗證碼的識別
- 8.1 圖形驗證碼的識別
- 8.2 極驗滑動驗證碼的識別
- 8.3 點觸驗證碼的識別
- 8.4微博宮格驗證碼的識別
- 9.代理的使用
- 9.1 代理的設置
- 9.2 代理池的維護
- 9.3 付費代理的使用
- 9.4 ADSL撥號代理
- 9.5 使用代理爬取微信公總號文章
- 10.模擬登錄
- 10.1 模擬登陸并爬去GitHub
- 10.2 Cookies池的搭建
- 11.App的爬取
- 11.1 Charles的使用
- 11.2 mitmproxy的使用
- 11.3 mitmdump“得到”App電子書信息
- 11.4 Appium的基本使用
- 11.5 Appnium爬取微信朋友圈
- 11.6 Appium+mitmdump爬取京東商品
- 12.pyspider框架的使用
- 12.1 pyspider框架介紹
- 12.2 pyspider的基本使用
- 12.3 pyspider用法詳解
- 13.Scrapy框架的使用
- 13.1 scrapy框架介紹
- 13.2 入門
- 13.3 selector的用法
- 13.4 spider的用法
- 13.5 Downloader Middleware的用法
- 13.6 Spider Middleware的用法
- 13.7 Item Pipeline的用法
- 13.8 Scrapy對接Selenium
- 13.9 Scrapy對接Splash
- 13.10 Scrapy通用爬蟲
- 13.11 Scrapyrt的使用
- 13.12 Scrapy對接Docker
- 13.13 Scrapy爬取新浪微博
- 14.分布式爬蟲
- 14.1 分布式爬蟲原理
- 14.2 Scrapy-Redis源碼解析
- 14.3 Scrapy分布式實現
- 14.4 Bloom Filter的對接
- 15.分布式爬蟲的部署
- 15.1 Scrapyd分布式部署
- 15.2 Scrapyd-Client的使用
- 15.3 Scrapyd對接Docker
- 15.4 Scrapyd批量部署
- 15.5 Gerapy分布式管理
- 微信公總號文章實戰
- 源碼
- other