最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

19. Scrapy 數(shù)據(jù)的保存

2020-07-02 10:39 作者:自學(xué)Python的小姐姐呀  | 我要投稿

1. 數(shù)據(jù)的提取

1.1 控制臺打印

import scrapy


class DoubanSpider(scrapy.Spider):
? ?name = 'douban'
? ?allwed_url = 'douban.com'
? ?start_urls = [
? ? ? ?'https://movie.douban.com/top250/'
? ?]

? ?def parse(self, response):
? ? ? ?movie_name = response.xpath("//div[@class='item']//a/span[1]/text()").extract()
? ? ? ?movie_core = response.xpath("//div[@class='star']/span[2]/text()").extract()
? ? ? ?yield {
? ? ? ? ? ?'movie_name':movie_name,
? ? ? ? ? ?'movie_core':movie_core
? ? ? ?}

執(zhí)行以上代碼,我可以在控制看到:


2018-01-24 15:17:14 [scrapy.utils.log] INFO: Scrapy 1.5.0 started (bot: spiderdemo1)
2018-01-24 15:17:14 [scrapy.utils.log] INFO: Versions: lxml 4.1.1.0, libxml2 2.9.5, cssselect 1.0.3, parsel 1.3.1, w3lib 1.18.0, Twiste
d 17.9.0, Python 3.6.3 (v3.6.3:2c5fed8, Oct ?3 2017, 18:11:49) [MSC v.1900 64 bit (AMD64)], pyOpenSSL 17.5.0 (OpenSSL 1.1.0g ?2 Nov 201
7), cryptography 2.1.4, Platform Windows-10-10.0.10240-SP0
2018-01-24 15:17:14 [scrapy.crawler] INFO: Overridden settings: {'BOT_NAME': 'spiderdemo1', 'NEWSPIDER_MODULE': 'spiderdemo1.spiders',
'ROBOTSTXT_OBEY': True, 'SPIDER_MODULES': ['spiderdemo1.spiders']}
2018-01-24 15:17:14 [scrapy.middleware] INFO: Enabled extensions:
['scrapy.extensions.corestats.CoreStats',
'scrapy.extensions.telnet.TelnetConsole',
'scrapy.extensions.logstats.LogStats']
2018-01-24 15:17:14 [scrapy.middleware] INFO: Enabled downloader middlewares:
['scrapy.downloadermiddlewares.robotstxt.RobotsTxtMiddleware',
'scrapy.downloadermiddlewares.httpauth.HttpAuthMiddleware',
'scrapy.downloadermiddlewares.downloadtimeout.DownloadTimeoutMiddleware',
'scrapy.downloadermiddlewares.defaultheaders.DefaultHeadersMiddleware',
'scrapy.downloadermiddlewares.useragent.UserAgentMiddleware',
'scrapy.downloadermiddlewares.retry.RetryMiddleware',
'scrapy.downloadermiddlewares.redirect.MetaRefreshMiddleware',
'scrapy.downloadermiddlewares.httpcompression.HttpCompressionMiddleware',
'scrapy.downloadermiddlewares.redirect.RedirectMiddleware',
'scrapy.downloadermiddlewares.cookies.CookiesMiddleware',
'scrapy.downloadermiddlewares.httpproxy.HttpProxyMiddleware',
'scrapy.downloadermiddlewares.stats.DownloaderStats']
2018-01-24 15:17:14 [scrapy.middleware] INFO: Enabled spider middlewares:
['scrapy.spidermiddlewares.httperror.HttpErrorMiddleware',
'scrapy.spidermiddlewares.offsite.OffsiteMiddleware',
'scrapy.spidermiddlewares.referer.RefererMiddleware',
'scrapy.spidermiddlewares.urllength.UrlLengthMiddleware',
'scrapy.spidermiddlewares.depth.DepthMiddleware']
2018-01-24 15:17:14 [scrapy.middleware] INFO: Enabled item pipelines:
[]
2018-01-24 15:17:14 [scrapy.core.engine] INFO: Spider opened
2018-01-24 15:17:14 [scrapy.extensions.logstats] INFO: Crawled 0 pages (at 0 pages/min), Scraped 0 items (at 0 items/min)
2018-01-24 15:17:14 [scrapy.extensions.telnet] DEBUG: Telnet console listening on 127.0.0.1:6023
2018-01-24 15:17:14 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://movie.douban.com/robots.txt> (referer: None)
2018-01-24 15:17:15 [scrapy.downloadermiddlewares.redirect] DEBUG: Redirecting (301) to <GET https://movie.douban.com/top250> from <GET
https://movie.douban.com/top250/>
2018-01-24 15:17:15 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://movie.douban.com/top250> (referer: None)
2018-01-24 15:17:15 [scrapy.core.scraper] DEBUG: Scraped from <200 https://movie.douban.com/top250>
{'movie_name': ['肖申克的救贖', '霸王別姬', '這個殺手不太冷', '阿甘正傳', '美麗人生', '千與千尋', '泰坦尼克號', '辛德勒的名單', '盜夢空
間', '機器人總動員', '海上鋼琴師', '三傻大鬧寶萊塢', '忠犬八公的故事', '放牛班的春天', '大話西游之大圣娶親', '教父', '龍貓', '楚門的世
界', '亂世佳人', '熔爐', '觸不可及', '天堂電影院', '當幸福來敲門', '無間道', '星際穿越'], 'movie_core': ['9.6', '9.5', '9.4', '9.4', '9
.5', '9.2', '9.2', '9.4', '9.3', '9.3', '9.2', '9.1', '9.2', '9.2', '9.2', '9.2', '9.1', '9.1', '9.2', '9.2', '9.1', '9.1', '8.9', '9.0
', '9.1']}
2018-01-24 15:17:15 [scrapy.core.engine] INFO: Closing spider (finished)
2018-01-24 15:17:15 [scrapy.statscollectors] INFO: Dumping Scrapy stats:
{'downloader/request_bytes': 651,
'downloader/request_count': 3,
'downloader/request_method_count/GET': 3,
'downloader/response_bytes': 13900,
'downloader/response_count': 3,
'downloader/response_status_count/200': 2,
'downloader/response_status_count/301': 1,
'finish_reason': 'finished',
'finish_time': datetime.datetime(2018, 1, 24, 7, 17, 15, 247183),
'item_Scraped_count': 1,
'log_count/DEBUG': 5,
'log_count/INFO': 7,
'response_received_count': 2,
'scheduler/dequeued': 2,
'scheduler/dequeued/memory': 2,
'scheduler/enqueued': 2,
'scheduler/enqueued/memory': 2,
'start_time': datetime.datetime(2018, 1, 24, 7, 17, 14, 784782)}
2018-01-24 15:17:15 [scrapy.core.engine] INFO: Spider closed (finished)

1.2 以文件的方式輸出

1.2.1 python原生方式

with open("movie.txt", 'wb') as f:
? ?for n, c in zip(movie_name, movie_core):
? ? ? ?str = n+":"+c+"\n"
? ? ? ?f.write(str.encode())

1.2.2 以scrapy內(nèi)置方式

scrapy 內(nèi)置主要有四種:JSON,JSON lines,CSV,XML

我們將結(jié)果用最常用的JSON導(dǎo)出,命令如下:

scrapy crawl dmoz -o douban.json -t json ?

-o 后面是導(dǎo)出文件名,-t 后面是導(dǎo)出類型

2 提取內(nèi)容的封裝Item

Scrapy進程可通過使用蜘蛛提取來自網(wǎng)頁中的數(shù)據(jù)。Scrapy使用Item類生成輸出對象用于收刮數(shù)據(jù)

Item 對象是自定義的python字典,可以使用標準字典語法獲取某個屬性的值

2.1 定義

import scrapy


class InfoItem(scrapy.Item):
? ?# define the fields for your item here like:
? ?movie_name = scrapy.Field()
? ?movie_core = scrapy.Field()

2.2 使用

def parse(self, response):
? ?movie_name = response.xpath("//div[@class='item']//a/span[1]/text()").extract()
? ?movie_core = response.xpath("//div[@class='star']/span[2]/text()").extract()
? ?
? ?for n, c in zip(movie_name, movie_core):
? ? ? ?movie = InfoItem()
? ? ? ?movie['movie_name'] = n
? ? ? ?movie['movie_core'] = c
? ? ? ?yield movie


19. Scrapy 數(shù)據(jù)的保存的評論 (共 條)

分享到微博請遵守國家法律
广宁县| 舟山市| 越西县| 延寿县| 寿阳县| 新疆| 普安县| 西乡县| 防城港市| 连城县| 陈巴尔虎旗| 万安县| 闸北区| 威宁| 华宁县| 灌阳县| 邢台县| 安龙县| 克拉玛依市| 从江县| 广安市| 金门县| 阿克| 南召县| 乌鲁木齐县| 海盐县| 柯坪县| 永丰县| 白河县| 邵武市| 通海县| 清水河县| 当雄县| 江源县| 从化市| 青铜峡市| 石狮市| 舒城县| 交城县| 舟山市| 杭锦旗|