主頁(yè) > 知識(shí)庫(kù) > python爬蟲(chóng)框架feapder的使用簡(jiǎn)介

python爬蟲(chóng)框架feapder的使用簡(jiǎn)介

熱門(mén)標(biāo)簽:百度AI接口 Win7旗艦版 企業(yè)做大做強(qiáng) 客戶(hù)服務(wù) 電話(huà)運(yùn)營(yíng)中心 硅谷的囚徒呼叫中心 呼叫中心市場(chǎng)需求 語(yǔ)音系統(tǒng)

1. 前言

大家好,我是安果!

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

今天推薦一款更加簡(jiǎn)單、輕量級(jí),且功能強(qiáng)大的爬蟲(chóng)框架:feapder

項(xiàng)目地址:

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

2. 介紹及安裝

和 Scrapy 類(lèi)似,feapder 支持輕量級(jí)爬蟲(chóng)、分布式爬蟲(chóng)、批次爬蟲(chóng)、爬蟲(chóng)報(bào)警機(jī)制等功能

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

  • AirSpider

輕量級(jí)爬蟲(chóng),適合簡(jiǎn)單場(chǎng)景、數(shù)據(jù)量少的爬蟲(chóng)

  • Spider

分布式爬蟲(chóng),基于 Redis,適用于海量數(shù)據(jù),并且支持?jǐn)帱c(diǎn)續(xù)爬、自動(dòng)數(shù)據(jù)入庫(kù)等功能

  • BatchSpider

分布式批次爬蟲(chóng),主要用于需要周期性采集的爬蟲(chóng)

在實(shí)戰(zhàn)之前,我們?cè)谔摂M環(huán)境下安裝對(duì)應(yīng)的依賴(lài)庫(kù)

# 安裝依賴(lài)庫(kù)
pip3 install feapder

3. 實(shí)戰(zhàn)一下

我們以最簡(jiǎn)單的 AirSpider 來(lái)爬取一些簡(jiǎn)單的數(shù)據(jù)

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

詳細(xì)實(shí)現(xiàn)步驟如下( 5 步)

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

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

# 創(chuàng)建一個(gè)爬蟲(chóng)項(xiàng)目
feapder create -p tophub_demo

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

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

cd spiders

# 創(chuàng)建一個(gè)輕量級(jí)爬蟲(chóng)
feapder create -s tophub_spider 1

其中

  • 1 為默認(rèn),表示創(chuàng)建一個(gè)輕量級(jí)爬蟲(chóng) AirSpider
  • 2 代表創(chuàng)建一個(gè)分布式爬蟲(chóng) Spider
  • 3 代表創(chuàng)建一個(gè)分布式批次爬蟲(chóng) BatchSpider

3-3  配置數(shù)據(jù)庫(kù)、創(chuàng)建數(shù)據(jù)表、創(chuàng)建映射 Item

以 Mysql 為例,首先我們?cè)跀?shù)據(jù)庫(kù)中創(chuàng)建一張數(shù)據(jù)表

# 創(chuàng)建一張數(shù)據(jù)表
create table topic
(
    id         int auto_increment
        primary key,
    title      varchar(100)  null comment '文章標(biāo)題',
    auth       varchar(20)   null comment '作者',
    like_count     int default 0 null comment '喜歡數(shù)',
    collection int default 0 null comment '收藏?cái)?shù)',
    comment    int default 0 null comment '評(píng)論數(shù)'
);

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

# settings.py

MYSQL_IP = "localhost"
MYSQL_PORT = 3306
MYSQL_DB = "xag"
MYSQL_USER_NAME = "root"
MYSQL_USER_PASS = "root"

最后,創(chuàng)建映射 Item( 可選 )

進(jìn)入到 items 文件夾,使用「 feapder create -i 」命令創(chuàng)建一個(gè)文件映射到數(shù)據(jù)庫(kù)

PS:由于 AirSpider 不支持?jǐn)?shù)據(jù)自動(dòng)入庫(kù),所以這步不是必須

3-4  編寫(xiě)爬蟲(chóng)及數(shù)據(jù)解析

第一步,首先使「 MysqlDB 」初始化數(shù)據(jù)庫(kù)

from feapder.db.mysqldb import MysqlDB

class TophubSpider(feapder.AirSpider):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.db = MysqlDB()

第二步,在 start_requests 方法中,指定爬取主鏈接地址,使用關(guān)鍵字「download_midware 」配置隨機(jī) 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):
    # 隨機(jī)UA
    # 依賴(lài):pip3 install fake_useragent
    ua = UserAgent().random
    request.headers = {'User-Agent': ua}
    return request

第三步,爬取首頁(yè)標(biāo)題、鏈接地址

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

def parse(self, request, response):
    # print(response.text)
    card_elements = response.xpath('//div[@class="cc-cd"]')

    # 過(guò)濾出對(duì)應(yīng)的卡片元素【什么值得買(mǎi)】
    buy_good_element = [card_element for card_element in card_elements if
                        card_element.xpath('.//div[@class="cc-cd-is"]//span/text()').extract_first() == '什么值得買(mǎi)'][0]

    # 獲取內(nèi)部文章標(biāo)題及地址
    a_elements = buy_good_element.xpath('.//div[@class="cc-cd-cb nano"]//a')

    for a_element in a_elements:
        # 標(biāo)題和鏈接
        title = a_element.xpath('.//span[@class="t"]/text()').extract_first()
        href = a_element.xpath('.//@href').extract_first()

        # 再次下發(fā)新任務(wù),并帶上文章標(biāo)題
        yield feapder.Request(href, download_midware=self.download_midware, callback=self.parser_detail_page,
                              title=title)

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

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

def parser_detail_page(self, request, response):
    """
    解析文章詳情數(shù)據(jù)
    :param request:
    :param response:
    :return:
    """
    title = request.title

    url = request.url

    # 解析文章詳情頁(yè)面,獲取點(diǎn)贊、收藏、評(píng)論數(shù)目及作者名稱(chēng)
    author = response.xpath('//a[@class="author-title"]/text()').extract_first().strip()

    print("作者:", author, '文章標(biāo)題:', title, "地址:", url)

    desc_elements = response.xpath('//span[@class="xilie"]/span')

    print("desc數(shù)目:", len(desc_elements))

    # 點(diǎn)贊
    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])
    # 評(píng)論
    comment_count = int(re.findall('\d+', desc_elements[3].xpath('./text()').extract_first())[0])

    print("點(diǎn)贊:", like_count, "收藏:", collection_count, "評(píng)論:", comment_count)

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

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

# 插入數(shù)據(jù)庫(kù)
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. 最后

本篇文章通過(guò)一個(gè)簡(jiǎn)單的實(shí)例,聊到了 feapder 中最簡(jiǎn)單的爬蟲(chóng) AirSpider

關(guān)于 feapder 高級(jí)功能的使用,后面我將會(huì)通過(guò)一系列實(shí)例進(jìn)行詳細(xì)說(shuō)明

源碼地址:https://github.com/xingag/spider_python/tree/master/feapder

以上就是python爬蟲(chóng)框架feapder的使用簡(jiǎn)介的詳細(xì)內(nèi)容,更多關(guān)于python爬蟲(chóng)框架feapde的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

您可能感興趣的文章:
  • 上手簡(jiǎn)單,功能強(qiáng)大的Python爬蟲(chóng)框架——feapder
  • Python爬蟲(chóng)框架之Scrapy中Spider的用法
  • Python爬蟲(chóng)框架-scrapy的使用
  • 一文讀懂python Scrapy爬蟲(chóng)框架
  • python Scrapy爬蟲(chóng)框架的使用
  • 詳解Python的爬蟲(chóng)框架 Scrapy
  • python3 Scrapy爬蟲(chóng)框架ip代理配置的方法
  • Python使用Scrapy爬蟲(chóng)框架全站爬取圖片并保存本地的實(shí)現(xiàn)代碼
  • Python爬蟲(chóng)框架Scrapy實(shí)例代碼
  • Python之Scrapy爬蟲(chóng)框架安裝及簡(jiǎn)單使用詳解
  • 爬蟲(chóng)框架 Feapder 和 Scrapy 的對(duì)比分析

標(biāo)簽:安康 長(zhǎng)沙 濟(jì)南 崇左 喀什 山西 海南 山西

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《python爬蟲(chóng)框架feapder的使用簡(jiǎn)介》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話(huà)咨詢(xún)

    • 400-1100-266