日誌等級與請求傳參
阿新 • • 發佈:2018-10-06
tin proc ons pipeline define .html request roi 爬蟲
日誌等級:
ERROR : 一般錯誤
WARNING : 警告
INFO : 一般的信息
DEBUG : 調試信息
默認的顯示級別是DEBUG
可以在配置文件中settings中進行日誌信息的指定輸出:
#指定日誌信息的輸出
#LOG_LEVEL = ‘DEBUG‘
#將日誌信息寫入到文件中進行存儲
LOG_FILE = ‘log.txt‘
二.請求傳參
- 在某些情況下,我們爬取的數據不在同一個頁面中,例如,我們爬取一個電影網站,電影的名稱,評分在一級頁面,而要爬取的其他電影詳情在其二級子頁面中。這時我們就需要用到請求傳參。
- 案例展示:爬取www.id97.com電影網,將一級頁面中的電影名稱,類型,評分一級二級頁面中的上映時間,導演,片長進行爬取。
# -*- coding: utf-8 -*- 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
# -*- 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()items.py
# -*- 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()管道文件
日誌等級與請求傳參