python爬蟲框架feapder的使用簡介
大家好,我是安果!
眾所周知,Python 最流行的爬蟲框架是 Scrapy,它主要用于爬取網站結構性數據
今天推薦一款更加簡單、輕量級,且功能強大的爬蟲框架:feapder
項目地址:
https://github.com/Boris-code/feapder
2. 介紹及安裝和 Scrapy 類似,feapder 支持輕量級爬蟲、分布式爬蟲、批次爬蟲、爬蟲報警機制等功能
內置的 3 種爬蟲如下:
AirSpider輕量級爬蟲,適合簡單場景、數據量少的爬蟲
Spider分布式爬蟲,基于 Redis,適用于海量數據,并且支持斷點續爬、自動數據入庫等功能
BatchSpider分布式批次爬蟲,主要用于需要周期性采集的爬蟲
在實戰之前,我們在虛擬環境下安裝對應的依賴庫
# 安裝依賴庫pip3 install feapder3. 實戰一下
我們以最簡單的 AirSpider 來爬取一些簡單的數據
目標網站:aHR0cHM6Ly90b3BodWIudG9kYXkvIA==
詳細實現步驟如下( 5 步)
3-1 創建爬蟲項目首先,我們使用「 feapder create -p 」命令創建一個爬蟲項目
# 創建一個爬蟲項目feapder create -p tophub_demo3-2 創建爬蟲 AirSpider
命令行進入到 spiders 文件夾目錄下,使用「 feapder create -s 」命令創建一個爬蟲
cd spiders# 創建一個輕量級爬蟲feapder create -s tophub_spider 1
其中
1 為默認,表示創建一個輕量級爬蟲 AirSpider 2 代表創建一個分布式爬蟲 Spider 3 代表創建一個分布式批次爬蟲 BatchSpider3-3 配置數據庫、創建數據表、創建映射 Item以 Mysql 為例,首先我們在數據庫中創建一張數據表
# 創建一張數據表create table topic( id int auto_incrementprimary key, title varchar(100) null comment ’文章標題’, auth varchar(20) null comment ’作者’, like_count int default 0 null comment ’喜歡數’, collection int default 0 null comment ’收藏數’, comment int default 0 null comment ’評論數’);
然后,打開項目根目錄下的 settings.py 文件,配置數據庫連接信息
# settings.pyMYSQL_IP = 'localhost'MYSQL_PORT = 3306MYSQL_DB = 'xag'MYSQL_USER_NAME = 'root'MYSQL_USER_PASS = 'root'
最后,創建映射 Item( 可選 )
進入到 items 文件夾,使用「 feapder create -i 」命令創建一個文件映射到數據庫
PS:由于 AirSpider 不支持數據自動入庫,所以這步不是必須
3-4 編寫爬蟲及數據解析第一步,首先使「 MysqlDB 」初始化數據庫
from feapder.db.mysqldb import MysqlDBclass TophubSpider(feapder.AirSpider): def __init__(self, *args, **kwargs):super().__init__(*args, **kwargs)self.db = MysqlDB()
第二步,在 start_requests 方法中,指定爬取主鏈接地址,使用關鍵字「download_midware 」配置隨機 UA
import feapderfrom fake_useragent import UserAgentdef start_requests(self): yield feapder.Request('https://tophub.today/', download_midware=self.download_midware)def download_midware(self, request): # 隨機UA # 依賴:pip3 install fake_useragent ua = UserAgent().random request.headers = {’User-Agent’: ua} return request
第三步,爬取首頁標題、鏈接地址
使用 feapder 內置方法 xpath 去解析數據即可
def parse(self, request, response): # print(response.text) card_elements = response.xpath(’//div[@class='cc-cd']’) # 過濾出對應的卡片元素【什么值得買】 buy_good_element = [card_element for card_element in card_elements ifcard_element.xpath(’.//div[@class='cc-cd-is']//span/text()’).extract_first() == ’什么值得買’][0] # 獲取內部文章標題及地址 a_elements = buy_good_element.xpath(’.//div[@class='cc-cd-cb nano']//a’) for a_element in a_elements:# 標題和鏈接title = a_element.xpath(’.//span[@class='t']/text()’).extract_first()href = a_element.xpath(’.//@href’).extract_first()# 再次下發新任務,并帶上文章標題yield feapder.Request(href, download_midware=self.download_midware, callback=self.parser_detail_page, title=title)
第四步,爬取詳情頁面數據
上一步下發新的任務,通過關鍵字「 callback 」指定回調函數,最后在 parser_detail_page 中對詳情頁面進行數據解析
def parser_detail_page(self, request, response): ''' 解析文章詳情數據 :param request: :param response: :return: ''' title = request.title url = request.url # 解析文章詳情頁面,獲取點贊、收藏、評論數目及作者名稱 author = response.xpath(’//a[@class='author-title']/text()’).extract_first().strip() print('作者:', author, ’文章標題:’, title, '地址:', url) desc_elements = response.xpath(’//span[@class='xilie']/span’) print('desc數目:', len(desc_elements)) # 點贊 like_count = int(re.findall(’d+’, desc_elements[1].xpath(’./text()’).extract_first())[0]) # 收藏 collection_count = int(re.findall(’d+’, desc_elements[2].xpath(’./text()’).extract_first())[0]) # 評論 comment_count = int(re.findall(’d+’, desc_elements[3].xpath(’./text()’).extract_first())[0]) print('點贊:', like_count, '收藏:', collection_count, '評論:', comment_count)3-5 數據入庫
使用上面實例化的數據庫對象執行 SQL,將數據插入到數據庫中即可
# 插入數據庫sql = 'INSERT INTO topic(title,auth,like_count,collection,comment) values(’%s’,’%s’,’%s’,’%d’,’%d’)' % (title, author, like_count, collection_count, comment_count)# 執行self.db.execute(sql)4. 最后
本篇文章通過一個簡單的實例,聊到了 feapder 中最簡單的爬蟲 AirSpider
關于 feapder 高級功能的使用,后面我將會通過一系列實例進行詳細說明
源碼地址:https://github.com/xingag/spider_python/tree/master/feapder
以上就是python爬蟲框架feapder的使用簡介的詳細內容,更多關于python爬蟲框架feapde的資料請關注好吧啦網其它相關文章!
相關文章:
1. Python獲取抖音關注列表封號賬號的實現代碼2. Warning: require(): open_basedir restriction in effect,目錄配置open_basedir報錯問題分析3. php網絡安全中命令執行漏洞的產生及本質探究4. 解決Python 進程池Pool中一些坑5. php測試程序運行速度和頁面執行速度的代碼6. Python如何讀寫CSV文件7. 三個不常見的 HTML5 實用新特性簡介8. ajax請求添加自定義header參數代碼9. python利用os模塊編寫文件復制功能——copy()函數用法10. 無線標記語言(WML)基礎之WMLScript 基礎第1/2頁
