python爬蟲框架feapder的使用簡介
1. 前言
大家好,我是安果!
眾所周知,Python 最流行的爬蟲框架是 Scrapy,它主要用于爬取網(wǎng)站結構性數(shù)據(jù)
今天推薦一款更加簡單、輕量級,且功能強大的爬蟲框架:feapder
項目地址:
https://github.com/Boris-code/feapder
2. 介紹及安裝
和 Scrapy 類似,feapder 支持輕量級爬蟲、分布式爬蟲、批次爬蟲、爬蟲報警機制等功能
內置的 3 種爬蟲如下:
- AirSpider
輕量級爬蟲,適合簡單場景、數(shù)據(jù)量少的爬蟲
- Spider
分布式爬蟲,基于 Redis,適用于海量數(shù)據(jù),并且支持斷點續(xù)爬、自動數(shù)據(jù)入庫等功能
- BatchSpider
分布式批次爬蟲,主要用于需要周期性采集的爬蟲
在實戰(zhàn)之前,我們在虛擬環(huán)境下安裝對應的依賴庫
# 安裝依賴庫 pip3 install feapder
3. 實戰(zhàn)一下
我們以最簡單的 AirSpider 來爬取一些簡單的數(shù)據(jù)
目標網(wǎng)站:aHR0cHM6Ly90b3BodWIudG9kYXkvIA==
詳細實現(xiàn)步驟如下( 5 步)
3-1 創(chuàng)建爬蟲項目
首先,我們使用「 feapder create -p 」命令創(chuàng)建一個爬蟲項目
# 創(chuàng)建一個爬蟲項目 feapder create -p tophub_demo
3-2 創(chuàng)建爬蟲 AirSpider
命令行進入到 spiders 文件夾目錄下,使用「 feapder create -s 」命令創(chuàng)建一個爬蟲
cd spiders # 創(chuàng)建一個輕量級爬蟲 feapder create -s tophub_spider 1
其中
- 1 為默認,表示創(chuàng)建一個輕量級爬蟲 AirSpider
- 2 代表創(chuàng)建一個分布式爬蟲 Spider
- 3 代表創(chuàng)建一個分布式批次爬蟲 BatchSpider
3-3 配置數(shù)據(jù)庫、創(chuàng)建數(shù)據(jù)表、創(chuàng)建映射 Item
以 Mysql 為例,首先我們在數(shù)據(jù)庫中創(chuàng)建一張數(shù)據(jù)表
# 創(chuàng)建一張數(shù)據(jù)表 create table topic ( id int auto_increment primary key, title varchar(100) null comment '文章標題', auth varchar(20) null comment '作者', like_count int default 0 null comment '喜歡數(shù)', collection int default 0 null comment '收藏數(shù)', comment int default 0 null comment '評論數(shù)' );
然后,打開項目根目錄下的 settings.py 文件,配置數(shù)據(jù)庫連接信息
# settings.py MYSQL_IP = "localhost" MYSQL_PORT = 3306 MYSQL_DB = "xag" MYSQL_USER_NAME = "root" MYSQL_USER_PASS = "root"
最后,創(chuàng)建映射 Item( 可選 )
進入到 items 文件夾,使用「 feapder create -i 」命令創(chuàng)建一個文件映射到數(shù)據(jù)庫
PS:由于 AirSpider 不支持數(shù)據(jù)自動入庫,所以這步不是必須
3-4 編寫爬蟲及數(shù)據(jù)解析
第一步,首先使「 MysqlDB 」初始化數(shù)據(jù)庫
from feapder.db.mysqldb import MysqlDB class TophubSpider(feapder.AirSpider): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.db = MysqlDB()
第二步,在 start_requests 方法中,指定爬取主鏈接地址,使用關鍵字「download_midware 」配置隨機 UA
import feapder
from fake_useragent import UserAgent
def 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 去解析數(shù)據(jù)即可
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 if
card_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()
# 再次下發(fā)新任務,并帶上文章標題
yield feapder.Request(href, download_midware=self.download_midware, callback=self.parser_detail_page,
title=title)
第四步,爬取詳情頁面數(shù)據(jù)
上一步下發(fā)新的任務,通過關鍵字「 callback 」指定回調函數(shù),最后在 parser_detail_page 中對詳情頁面進行數(shù)據(jù)解析
def parser_detail_page(self, request, response):
"""
解析文章詳情數(shù)據(jù)
:param request:
:param response:
:return:
"""
title = request.title
url = request.url
# 解析文章詳情頁面,獲取點贊、收藏、評論數(shù)目及作者名稱
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數(shù)目:", 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 數(shù)據(jù)入庫
使用上面實例化的數(shù)據(jù)庫對象執(zhí)行 SQL,將數(shù)據(jù)插入到數(shù)據(jù)庫中即可
# 插入數(shù)據(jù)庫
sql = "INSERT INTO topic(title,auth,like_count,collection,comment) values('%s','%s','%s','%d','%d')" % (
title, author, like_count, collection_count, comment_count)
# 執(zhí)行
self.db.execute(sql)
4. 最后
本篇文章通過一個簡單的實例,聊到了 feapder 中最簡單的爬蟲 AirSpider
關于 feapder 高級功能的使用,后面我將會通過一系列實例進行詳細說明
源碼地址:https://github.com/xingag/spider_python/tree/master/feapder
以上就是python爬蟲框架feapder的使用簡介的詳細內容,更多關于python爬蟲框架feapde的資料請關注腳本之家其它相關文章!
相關文章
PyQt5?python?數(shù)據(jù)庫?表格動態(tài)增刪改詳情
這篇文章主要介紹了PyQt5?python?數(shù)據(jù)庫?表格動態(tài)增刪改詳情,首先手動連接數(shù)據(jù)庫與下一個的程序連接數(shù)據(jù)庫是獨立的2個部分,下面來看看文章的詳細介紹2022-01-01
Python?中的對象析構函數(shù)__del__?詳情
這篇文章主要介紹了Python?中的對象析構函數(shù)del詳情,Python?中的類的構造函數(shù)???__init__???,?每當實例產(chǎn)生就會調用這個構造函下面更多相關內容,需要的小伙伴可以參考一下2022-03-03
python中超簡單的字符分割算法記錄(車牌識別、儀表識別等)
這篇文章主要給大家介紹了關于python中超簡單的字符分割算法記錄,如車牌識別、儀表識別等,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2021-09-09
PyTorch中torch.matmul()函數(shù)常見用法總結
torch.matmul()也是一種類似于矩陣相乘操作的tensor連乘操作。但是它可以利用python中的廣播機制,處理一些維度不同的tensor結構進行相乘操作,這篇文章主要介紹了PyTorch中torch.matmul()函數(shù)用法總結,需要的朋友可以參考下2023-04-04
Python中Collections模塊的Counter容器類使用教程
Counter是Python標準庫提供的一個非常有用的容器,可以用來對序列中出現(xiàn)的各個元素進行計數(shù),下面就來一起看一下Python中Collections模塊的Counter容器類使用教程2016-05-05

