scrapy實現多級頁面爬取(初級練習題)

2021-08-21 10:20:35 字數 3832 閱讀 1333

練習題:quotes to scrapes【諺語**】   等級:初級

爬取每條諺語的資訊(諺語、作者、標籤、作者出生日期、作者出事地點、作者基本描述)

思路:

2、得到初始url的response,傳遞給parse1函式(負責解析第一級頁面),解析response;

4、parse2函式會解析每個二級頁面的url的response,得到最終資料;

易忽略點:

1、因為諺語的二級頁面的url是根據作者來定義url路徑的,因此有很多重複的二級url,需要不去重操作;

next_url = response.xpath('//ul[@class="pager"]//a/@href').extract()[0]

if next_url is not none:

yield scrapy.request(url=self.base_url + next_url, callback=self.parse)

這樣寫next_url獲取到的是前一頁的url,if裡的條件永遠滿足。分析元素樹結構的時候忽略了還有前一頁的元素導致的。正確的next_url**見下方。

準備工作:新建專案、新建爬蟲

明確目標:item.py

import scrapy

class quotestoscrapeitem(scrapy.item):

# define the fields for your item here like:

# name = scrapy.field()

# 名言

quote = scrapy.field()

# 作者

author = scrapy.field()

# 標籤

tags = scrapy.field()

# 出生日期

born_date = scrapy.field()

# 出生位置

born_location = scrapy.field()

# 描述

description = scrapy.field()

定義爬蟲:quotes.py

[去重機制:request的引數dont_filter預設是false(去重),每yield乙個request,就將url引數與排程器內已有的url進行比較,如果存在相同url則預設不入佇列,如果沒有相同的url則入佇列,每乙個url入佇列前都要與現有的url進行比較。如果想要實現不去重效果,則將dont_filter改為true]

# -*- coding: utf-8 -*-

import scrapy

from quotes_toscrape.items import quotestoscrapeitem

class quotesspider(scrapy.spider):

name = 'quotes'

allowed_domains = ['quotes.toscrape.com']

# start_urls = ['']

base_url = ''

page = 1

first_url ='page/{}/'

start_urls = [first_url.format(page)]

def parse(self, response):

node_list = response.xpath('//div[@class="quote"]')

for node in node_list:

quote = node.xpath('.//span[@class="text"]/text()').extract()[0][1:-1]

author = node.xpath('.//small/text()').extract()[0]

tags = node_list.xpath('.//div[@class="tags"]//a/text()').extract()[0]

href = self.base_url + node.xpath('.//small/following-sibling::a/@href').extract()[0]

yield scrapy.request(url=href, meta=,

callback=self.parse_author, dont_filter=true)

next_url = response.xpath('//ul[@class="pager"]/li[@class="next"]/a/@href').extract()[0]

if next_url is not none:

yield scrapy.request(url=self.base_url + next_url, callback=self.parse)

# if self.page<10:

# self.page += 1

# yield scrapy.request(url=self.first_url.format(self.page), callback=self.parse)

def parse_author(self,response):

item = quotestoscrapeitem()

# 組合資訊

item['quote'] = response.meta['quote']

item['author'] = response.meta['author']

item['tags'] = response.meta['tags']

item['born_date'] = response.xpath('//span[@class="author-born-date"]/text()').extract()[0]

item['born_location'] = response.xpath('//span[@class="author-born-location"]/text()').extract()[0][3:]

# 去掉前後空格

item['description'] = response.xpath('//div[@class="author-description"]/text()').extract()[0].strip()

yield item

定義管道:pipelines.py

import json

class quotestoscrapepipeline(object):

def __init__(self):

self.file = open('quotes.json','wb')

def process_item(self, item, spider):

data = json.dumps(dict(item),ensure_ascii=false,indent=4) +','

# 編碼

self.file.write(data.encode('utf-8'))

return item

def close_spider(self,spider):

self.file.close()

將注釋的**item_pipelines開啟即可。

item_pipelines =
執行爬蟲:scrapy crawl quotes

得到結果:quotes.json

scrapy 爬取流程

什麼時候到pipeline,什麼 時候到spider這個就不說了,這個是框架跳轉到的流程 關鍵是訪問之前要登入怎麼辦,資料還要注入呢 這是個列表,裡面就是爬取的鏈結了 我們前面爬取就只是寫了乙個,但是其實可以寫多個 鏈結又是怎麼訪問的呢 這東西你就可以手動提取鏈結返回了 這東西你就得好好注意了 從入...

scrapy 爬取小說

速度是相當的快的 爬取整站的 最後結果儲存至mongodb資料庫 pycharm開發還是很好用的 建立專案 scrapy startproject daomubiji 執行專案 scrapy crawl daomubi settings default request headers items t...

scrapy爬取噹噹

import scrapy from items import dangdangitem class ddspider scrapy.spider name dd allowed domains dangdang.com start urls def parse self,response 使用xp...