上次用requests寫的爬蟲速度很感人,今天打算用scrapy框架來實現,看看速度如何。
爬蟲步驟
第一步,安裝scrapy,執行一下命令
pip install Scrapy
第二步,創建項目,執行一下命令
scrapy startproject novel
第三步,編寫spider文件,文件存放位置novel/spiders/toscrape-xpath.py,內容如下
# -*- coding: utf-8 -*-
import scrapy
class ToScrapeSpiderXPath(scrapy.Spider):
# 爬蟲的名字
name = 'novel'
# 爬蟲啓始url
start_urls = [
'https://www.xbiquge6.com/0_638/1124120.html',
]
def parse(self, response):
# 定義存儲的數據格式
yield {
'text': response.xpath('//div[@class="bookname"]/h1[1]/text()').extract_first(),
'content': response.xpath('//div[@id="content"]/text()').extract(),
# 'author': quote.xpath('.//small[@class="author"]/text()').extract_first(),
# 'tags': quote.xpath('.//div[@class="tags"]/a[@class="tag"]/text()').extract()
}
# 下一章的鏈接
next_page_url = response.xpath('//div[@class="bottem1"]/a[3]/@href').extract_first()
# 如果下一章的鏈接不等於首頁 則爬取url內容 ps:最後一章的下一章鏈接爲首頁
if next_page_url != 'https://www.xbiquge6.com/0_638/':
yield scrapy.Request(response.urljoin(next_page_url))
總結
框架用時:23分,比requests快三倍!awesmome!xpath也蠻好用的,繼續學習,歡迎交流。