scrapy框架之日誌等級和請求傳參
阿新 • • 發佈:2018-11-01
一.Scrapy的日誌等級
- 在使用scrapy crawl spiderFileName執行程式時,在終端裡列印輸出的就是scrapy的日誌資訊。
- 日誌資訊的種類:
ERROR : 一般錯誤
WARNING : 警告
INFO : 一般的資訊
DEBUG : 除錯資訊
預設的顯示級別是DEBUG
- 設定日誌資訊指定輸出:
在settings.py配置檔案中,加入LOG_LEVEL = ‘指定日誌資訊種類’即可。LOG_FILE = 'log.txt'則表示將日誌資訊寫入到指定檔案中進行儲存。
二.請求傳參
- 在某些情況下,我們爬取的資料不在同一個頁面中,例如,我們爬取一個電影網站,電影的名稱,評分在一級頁面,而要爬取的其他電影詳情在其二級子頁面中。這時我們就需要用到請求傳參。
ex:爬取www.id97.com電影網,將一級頁面中的電影名稱,型別,評分一級二級頁面中的上映時間,導演,片長進行爬取。
import scrapy from moviePro.items import MovieproItem class MovieSpider(scrapy.Spider): name = 'movie' allowed_domains= ['www.id97.com'] start_urls = ['http://www.id97.com/'] def parse(self, response): div_list = response.xpath('//div[@class="col-xs-1-5 movie-item"]') for div in div_list: item = MovieproItem() item['name'] = div.xpath('.//h1/a/text()').extract_first() item['score'] = div.xpath('.//h1/em/text()').extract_first() #xpath(string(.))表示提取當前節點下所有子節點中的資料值(.)表示當前節點 item['kind'] = div.xpath('.//div[@class="otherinfo"]').xpath('string(.)').extract_first() item['detail_url'] = div.xpath('./div/a/@href').extract_first() #請求二級詳情頁面,解析二級頁面中的相應內容,通過meta引數進行Request的資料傳遞 yield scrapy.Request(url=item['detail_url'],callback=self.parse_detail,meta={'item':item}) def parse_detail(self,response): #通過response獲取item item = response.meta['item'] item['actor'] = response.xpath('//div[@class="row"]//table/tr[1]/a/text()').extract_first() item['time'] = response.xpath('//div[@class="row"]//table/tr[7]/td[2]/text()').extract_first() item['long'] = response.xpath('//div[@class="row"]//table/tr[8]/td[2]/text()').extract_first() #提交item到管道 yield item
items.py:
-*- coding: utf-8 -*- # Define here the models for your scraped items # # See documentation in: # https://doc.scrapy.org/en/latest/topics/items.html import scrapy class MovieproItem(scrapy.Item): # define the fields for your item here like: name = scrapy.Field() score = scrapy.Field() time = scrapy.Field() long = scrapy.Field() actor = scrapy.Field() kind = scrapy.Field() detail_url = scrapy.Field()
管道檔案:
# -*- coding: utf-8 -*- # Define your item pipelines here # # Don't forget to add your pipeline to the ITEM_PIPELINES setting # See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html import json class MovieproPipeline(object): def __init__(self): self.fp = open('data.txt','w') def process_item(self, item, spider): dic = dict(item) print(dic) json.dump(dic,self.fp,ensure_ascii=False) return item def close_spider(self,spider): self.fp.close()