將 [CrawSpider](http://www.hmoore.net/king_om/py_1/2229599) 改寫成分布式爬蟲。
<br/>
步驟如下:
**1. 先創建普通的CrawSpider**
```
# scrapy genspider -t crawl <爬蟲名稱> <域名>
> scrapy genspider -t crawl ct_liks www.wxapp-union.com
```
<br/>
**2. 將普通的CrawlSpider改寫成分布式的**
```python
"""
ct_liks.py 文件名
"""
import scrapy
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule
# 1. 導入RedisCrawlSpider
from scrapy_redis.spiders import RedisCrawlSpider
# 2. 繼承 RedisCrawlSpider
# class CtLiksSpider(CrawlSpider):
class CtLiksSpider(RedisCrawlSpider):
name = 'ct_liks'
# 3. 注銷allowed_domains 和 start_urls
# allowed_domains = ['www.wxapp-union.com']
# start_urls = ['http://www.wxapp-union.com/']
# 4. 添加redis_key
redis_key = "ct_start_url"
rules = (
Rule(LinkExtractor(allow=r'www.wxapp-union.com/article-\d+-1.html'), callback='parse_item')
)
# 5. 在 __init__中定義allowed_domains
def __init__(self, *args, **kwargs):
domain = kwargs.pop('domain', '')
# 多個允許的域采用 , 分割
self.allowed_domains = list(filter(None, domain.split(',')))
super(CtLiksSpider, self).__init__(*args, **kwargs)
def parse_item(self, response):
title = response.xpath("//title").extract_first()
print(title)
```
<br/>
**3. ` settings.py`中設置分布式相關配置**
```python
###### 添加如下配置 #########
# 設置重復過濾器的模塊
DUPEFILTER_CLASS = "scrapy_redis.dupefilter.RFPDupeFilter"
# 設置調度器,調度器具備與redis數據庫交互的功能
SCHEDULER = "scrapy_redis.scheduler.Scheduler"
# 設置當爬蟲結束時是否保持redis數據庫中的去重集合與任務對象
# True: 保持
# False: 不保持,任務結束就會清空數據庫
SCHEDULER_PERSIST = True
#SCHEDULER_QUEUE_CLASS = "scrapy_redis.queue.SpiderPriorityQueue"
#SCHEDULER_QUEUE_CLASS = "scrapy_redis.queue.SpiderQueue"
#SCHEDULER_QUEUE_CLASS = "scrapy_redis.queue.SpiderStack"
ITEM_PIPELINES = {
'example.pipelines.ExamplePipeline': 300,
# 當開啟該管道,該管道將會自動把數據存儲到redis數據庫中
'scrapy_redis.pipelines.RedisPipeline': 400,
}
# 設置redis數據庫
REDIS_URL = "redis://localhost:6379"
# 或者采用下面這種方式設置
# REDIS_HOST = 'localhost'
# REDIS_PORT = 6379
LOG_LEVEL = 'DEBUG'
# Introduce an artifical delay to make use of parallelism. to speed up the
# crawl.
DOWNLOAD_DELAY = 1
```
<br/>
**4. 啟動爬蟲**
```shell
# domain用 , 分割
> scrapy runspider ct_liks.py domain='www.baidu.com,taobao.com'
```
<br/>
**5. 往Redis數據庫中放入 `start_urls`**
```shell
> lpush ct_start_url http://www.wxapp-union.com/
```
當爬蟲根據 `redis_key` 讀取到 `start_urls`后就開啟工作了。
- 爬蟲基本概念
- 爬蟲介紹
- 通用爬蟲與聚焦爬蟲
- 通用爬蟲
- 聚焦爬蟲
- HTTP與HTTPS協議
- HTTP協議簡介
- HTTP的請求與響應
- 客戶端HTTP請求
- 服務端HTTP響應
- requests庫
- requests庫簡介
- requests簡單使用
- 發送帶header的請求
- 發送帶參數的請求
- 案例:下載百度貼吧頁面
- 發送POST請求
- 使用代理
- 為什么要使用代理?
- 正反向代理
- 代理服務器分類
- 使用代理
- cookie和session
- cookie和session的區別
- 爬蟲處理cookie和session
- 使用session登錄網站
- 使用cookie登錄網站
- cookiejar
- 超時和重試
- verify參數忽略CA證書
- URL地址的解碼和編碼
- 數據處理
- json數據處理
- json數據處理方案
- json模塊處理json數據
- jsonpath處理json數據
- 正則表達式
- lxml
- xpath與lxml介紹
- xpathhelper插件
- 案例
- Beautiful Soup
- Beautiful Soup介紹
- 解析器
- CSS選擇器
- 案例
- 四大對象
- 爬蟲與反爬蟲
- 爬蟲與反爬蟲的斗爭
- 服務器反爬的原因
- 什么樣的爬蟲會被反爬
- 反爬領域常見概念
- 反爬的三個方向
- 基于身份識別進行反爬
- 基于爬蟲行為進行反爬
- 基于數據加密進行反爬
- js解析
- chrome瀏覽器使用
- 定位js
- 設置斷點
- js2py
- hashlib
- 有道翻譯案例
- 動態爬取HTML
- 動態HTML
- 獲取Ajax數據的方式
- selenium+driver
- driver定位
- 表單元素操作
- 行為鏈
- cookie操作
- 頁面等待
- 多窗口與頁面切換
- 配置對象
- 拉勾網案例
- 圖片驗證碼識別
- 圖形驗證碼識別技術簡介
- Tesseract
- pytesseract處理圖形驗證碼
- 打碼平臺
- 登錄打碼平臺
- 驗證碼種類
- 多任務-線程
- 繼承Thread創建線程
- 查看線程數量
- 資源共享
- 互斥鎖
- 死鎖
- 避免死鎖
- Queue線程
- 多線程爬蟲
- 多任務-進程
- 創建進程
- 進程池
- 進程間的通信
- Python GIL
- scrapy框架
- scrapy是什么?
- scrapy爬蟲流程
- 創建scrapy項目
- Selector選擇器
- logging
- scrapy shell
- 保存數據
- Item數據建模
- 翻頁請求
- Request
- CrawlSpider
- settings
- 模擬登錄
- 保存文件
- 內置Pipeline
- 自定義Pipeline
- 中間件
- selenium動態加載
- 防止反爬
- 隨機User-Agent
- 隨機IP代理
- settings中的參數
- 隨機延遲
- request.meta常用參數
- 分布式爬蟲
- 分布式原理
- scrapy_redis
- 去重問題
- 分布式爬蟲編寫流程
- CrawSpider改寫成分布式
- scrapy_splash
- scrapy_splash是什么?
- scrapy_splash環境搭建
- APP抓取
- Android模擬器
- appium
- appium是什么?
- appium環境搭建
- appium環境聯調測試
- appium的使用
- 演示項目-抓取抖音app
- 抖音app與appium的聯調測試
- 元素定位
- 抖音appium代碼
- 抓包軟件
- url去重處理