日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

爬虫开发10.scrapy框架之日志等级和请求传参

發布時間:2024/9/5 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 爬虫开发10.scrapy框架之日志等级和请求传参 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

今日概要

  • 日志等級
  • 請求傳參

今日詳情

一.Scrapy的日志等級

  - 在使用scrapy crawl spiderFileName運行程序時,在終端里打印輸出的就是scrapy的日志信息。

  - 日志信息的種類:

        ERROR : 一般錯誤

        WARNING : 警告

        INFO : 一般的信息

        DEBUG : 調試信息

       

  - 設置日志信息指定輸出:

    在settings.py配置文件中,加入

????????????????????LOG_LEVEL = ‘指定日志信息種類’即可。

????????????????????LOG_FILE = 'log.txt'則表示將日志信息寫入到指定文件中進行存儲。

二.請求傳參

  - 在某些情況下,我們爬取的數據不在同一個頁面中,例如,我們爬取一個電影網站,電影的名稱,評分在一級頁面,而要爬取的其他電影詳情在其二級子頁面中。這時我們就需要用到請求傳參。

  - 案例展示:爬取www.id97.com電影網,將一級頁面中的電影名稱,類型,評分一級二級頁面中的上映時間,導演,片長進行爬取。

  爬蟲文件:

# -*- coding: utf-8 -*- import scrapy from moviePro.items import MovieproItemclass 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獲取itemitem = 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文件:

# -*- coding: utf-8 -*-# Define here the models for your scraped items # # See documentation in: # https://doc.scrapy.org/en/latest/topics/items.htmlimport scrapyclass 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.htmlimport 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 itemdef close_spider(self,spider):self.fp.close()

轉載于:https://www.cnblogs.com/sunny666/p/10542647.html

與50位技術專家面對面20年技術見證,附贈技術全景圖

總結

以上是生活随笔為你收集整理的爬虫开发10.scrapy框架之日志等级和请求传参的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。