使用scrapy爬蟲,爬取起點小說網的案例

2022-06-13 22:12:15 字數 2949 閱讀 1327

爬取的頁面為

爬取的**為凡人修仙之仙界篇,這邊**很不錯。

正文的章節如下圖所示

其中下面的章節為加密部分,現在暫時無法破解加密的部分。ε=(´ο`*)))唉..

下面直接上最核心的**(位於spiders中的核心**)

#

-*- coding: utf-8 -*-

import

scrapy

from qidian.items import

qidianitem

import

enum

class

qidian1spider(scrapy.spider):

name = '

qidian1

'allowed_domains = ['

qidian.com']

start_urls = ['

']defparse(self, response):

#div[@class="volume"][1或者2或者3或者4]中的數值,這些數值自定義乙個變數替代,目前一共是4個部分,隨著後續章節的增加,會出現第五部分或者第六部分 依次累加

###div[@class="volume"]["num"] ,num是自定義的變數,你可以換成自己想要的abc或者bb等變數,把這些變數放進去,就能得到所有章節的title??(不知道為什麼)

for aa in

response.xpath(

'//div[@class="volume-wrap"]/div[@class="volume"]["''

這裡填啥都行,不填就報錯,或者去掉class=volume後面的這個中括號就得不到a標籤中的標題,我也不知道什麼原因!!!"]''

在items.py中的核心**為

#

-*- coding: utf-8 -*-

#define here the models for your scraped items##

see documentation in:

#import

scrapy

class

qidianitem(scrapy.item):

title =scrapy.field()

link =scrapy.field()

content = scrapy.field()

在pipelines.py中的核心**為

#

-*- coding: utf-8 -*-

import

json

class

qidianpipeline(object):

defprocess_item(self, item, spider):

return

item

#初始化時指定要操作的檔案

def__init__

(self):

self.file = open('

item.json

', '

w', encoding='

utf-8')

#儲存資料,將 item 例項作為 json 資料寫入到檔案中

defprocess_item(self, item, spider):

lines = json.dumps(dict(item), ensure_ascii=false) + '\n'

self.file.write(lines)

return

item

#處理結束後關閉 檔案 io 流

defclose_spider(self, spider):

self.file.close()

我們最後得到的結果為像這種的。

........

Scrapy爬蟲爬取電影天堂

目標 建立專案 scrapy startproject 爬蟲專案檔案的名字 生成 crawlspider 命令 scrapy genspider t crawl 爬蟲名字 爬蟲網域名稱 終端執行 scrapy crawl 爬蟲的名字 python操作mysql資料庫操作 爬蟲檔案 coding ut...

網路爬蟲(四) 使用Scrapy爬取網易新聞

import scrapy class newsitem scrapy.item news thread scrapy.field news title scrapy.field news url scrapy.field news time scrapy.field news source scr...

scrapy多爬蟲以及爬取速度

主要這段時間一直使用的就是scrapy這個框架,因為公司裡面需要爬取大量的 所以才使用了多爬蟲,但是目前測試也只是幾十個,一直也想不到更好的方法去同時抓取成千上百個結構不同的 所以也很是苦逼的用了scrapy裡面的多爬蟲,對每個 分別解析,還好雖然幾次改需求但是欄位都是統一的,可以很輕鬆的通過ite...