日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關咨詢
選擇下列產(chǎn)品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
介紹一款能取代Scrapy的爬蟲框架-feapder

 1. 前言

大家好,我是安果!

創(chuàng)新互聯(lián)公司成立以來不斷整合自身及行業(yè)資源、不斷突破觀念以使企業(yè)策略得到完善和成熟,建立了一套“以技術(shù)為基點,以客戶需求中心、市場為導向”的快速反應體系。對公司的主營項目,如中高端企業(yè)網(wǎng)站企劃 / 設計、行業(yè) / 企業(yè)門戶設計推廣、行業(yè)門戶平臺運營、成都APP應用開發(fā)、手機網(wǎng)站制作設計、微信網(wǎng)站制作、軟件開發(fā)、遂寧聯(lián)通機房等實行標準化操作,讓客戶可以直觀的預知到從創(chuàng)新互聯(lián)公司可以獲得的服務效果。

眾所周知,Python 最流行的爬蟲框架是 Scrapy,它主要用于爬取網(wǎng)站結(jié)構(gòu)性數(shù)據(jù)

今天推薦一款更加簡單、輕量級,且功能強大的爬蟲框架:feapder

項目地址:

https://github.com/Boris-code/feapder

2. 介紹及安裝

和 Scrapy 類似,feapder 支持輕量級爬蟲、分布式爬蟲、批次爬蟲、爬蟲報警機制等功能

內(nèi)置的 3 種爬蟲如下:

  •  AirSpider

    輕量級爬蟲,適合簡單場景、數(shù)據(jù)量少的爬蟲

  •  Spider

    分布式爬蟲,基于 Redis,適用于海量數(shù)據(jù),并且支持斷點續(xù)爬、自動數(shù)據(jù)入庫等功能

  •  BatchSpider

    分布式批次爬蟲,主要用于需要周期性采集的爬蟲

在實戰(zhàn)之前,我們在虛擬環(huán)境下安裝對應的依賴庫

 
 
 
  1. # 安裝依賴庫  
  2. pip3 install feapder 

3. 實戰(zhàn)一下

我們以最簡單的 AirSpider 來爬取一些簡單的數(shù)據(jù)

目標網(wǎng)站:aHR0cHM6Ly90b3BodWIudG9kYXkvIA==

詳細實現(xiàn)步驟如下( 5 步)

3-1  創(chuàng)建爬蟲項目

首先,我們使用「 feapder create -p 」命令創(chuàng)建一個爬蟲項目

 
 
 
  1. # 創(chuàng)建一個爬蟲項目  
  2. feapder create -p tophub_demo 

3-2  創(chuàng)建爬蟲 AirSpider

命令行進入到 spiders 文件夾目錄下,使用「 feapder create -s 」命令創(chuàng)建一個爬蟲

 
 
 
  1. cd spiders  
  2. # 創(chuàng)建一個輕量級爬蟲  
  3. 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ù)表

 
 
 
  1. # 創(chuàng)建一張數(shù)據(jù)表  
  2. create table topic  
  3. (  
  4.     id         int auto_increment  
  5.         primary key,  
  6.     title      varchar(100)  null comment '文章標題',  
  7.     auth       varchar(20)   null comment '作者',  
  8.     like_count     int default 0 null comment '喜歡數(shù)',  
  9.     collection int default 0 null comment '收藏數(shù)',  
  10.     comment    int default 0 null comment '評論數(shù)'  
  11. ); 

然后,打開項目根目錄下的 settings.py 文件,配置數(shù)據(jù)庫連接信息

 
 
 
  1. # settings.py  
  2. MYSQL_IP = "localhost"  
  3. MYSQL_PORT = 3306  
  4. MYSQL_DB = "xag"  
  5. MYSQL_USER_NAME = "root"  
  6. 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ù)庫

 
 
 
  1. from feapder.db.mysqldb import MysqlDB  
  2. class TophubSpider(feapder.AirSpider):  
  3.     def __init__(self, *args, **kwargs):  
  4.         super().__init__(*args, **kwargs)  
  5.         self.db = MysqlDB() 

第二步,在 start_requests 方法中,指定爬取主鏈接地址,使用關鍵字「download_midware 」配置隨機 UA

 
 
 
  1. import feapder  
  2. from fake_useragent import UserAgent  
  3. def start_requests(self):  
  4.     yield feapder.Request("https://tophub.today/", download_midware=self.download_midware)  
  5. def download_midware(self, request):  
  6.     # 隨機UA  
  7.     # 依賴:pip3 install fake_useragent  
  8.     ua = UserAgent().random  
  9.     request.headers = {'User-Agent': ua}  
  10.     return request 

第三步,爬取首頁標題、鏈接地址

使用 feapder 內(nèi)置方法 xpath 去解析數(shù)據(jù)即可

 
 
 
  1. def parse(self, request, response):  
  2.     # print(response.text)  
  3.     card_elements = response.xpath('//div[@class="cc-cd"]')  
  4.     # 過濾出對應的卡片元素【什么值得買】  
  5.     buy_good_element = [card_element for card_element in card_elements if  
  6.                         card_element.xpath('.//div[@class="cc-cd-is"]//span/text()').extract_first() == '什么值得買'][0]  
  7.     # 獲取內(nèi)部文章標題及地址  
  8.     a_elements = buy_good_element.xpath('.//div[@class="cc-cd-cb nano"]//a')  
  9.     for a_element in a_elements:  
  10.         # 標題和鏈接  
  11.         title = a_element.xpath('.//span[@class="t"]/text()').extract_first()  
  12.         href = a_element.xpath('.//@href').extract_first()  
  13.         # 再次下發(fā)新任務,并帶上文章標題  
  14.         yield feapder.Request(href, download_midware=self.download_midware, callback=self.parser_detail_page,  
  15.                               titletitle=title) 

第四步,爬取詳情頁面數(shù)據(jù)

上一步下發(fā)新的任務,通過關鍵字「 callback 」指定回調(diào)函數(shù),最后在 parser_detail_page 中對詳情頁面進行數(shù)據(jù)解析

 
 
 
  1. def parser_detail_page(self, request, response):  
  2.     """  
  3.     解析文章詳情數(shù)據(jù)  
  4.     :param request:  
  5.     :param response:  
  6.     :return:  
  7.     """  
  8.     title = request.title  
  9.     url = request.url  
  10.     # 解析文章詳情頁面,獲取點贊、收藏、評論數(shù)目及作者名稱  
  11.     author = response.xpath('//a[@class="author-title"]/text()').extract_first().strip()  
  12.     print("作者:", author, '文章標題:', title, "地址:", url)  
  13.     desc_elements = response.xpath('//span[@class="xilie"]/span')  
  14.     print("desc數(shù)目:", len(desc_elements))  
  15.     # 點贊  
  16.     like_count = int(re.findall('\d+', desc_elements[1].xpath('./text()').extract_first())[0])  
  17.     # 收藏 
  18.     collection_count = int(re.findall('\d+', desc_elements[2].xpath('./text()').extract_first())[0])  
  19.     # 評論  
  20.     comment_count = int(re.findall('\d+', desc_elements[3].xpath('./text()').extract_first())[0])  
  21.     print("點贊:", like_count, "收藏:", collection_count, "評論:", comment_count) 

3-5  數(shù)據(jù)入庫

使用上面實例化的數(shù)據(jù)庫對象執(zhí)行 SQL,將數(shù)據(jù)插入到數(shù)據(jù)庫中即可

 
 
 
  1. # 插入數(shù)據(jù)庫  
  2. sql = "INSERT INTO topic(title,auth,like_count,collection,comment) values('%s','%s','%s','%d','%d')" % (  
  3. title, author, like_count, collection_count, comment_count)  
  4. # 執(zhí)行  
  5. self.db.execute(sql) 

4. 最后

本篇文章通過一個簡單的實例,聊到了 feapder 中最簡單的爬蟲 AirSpider


名稱欄目:介紹一款能取代Scrapy的爬蟲框架-feapder
當前URL:http://m.5511xx.com/article/dhidjoe.html