Python使用mongodb保存爬取豆瓣電影的數(shù)據(jù)過(guò)程解析
創(chuàng)建爬蟲(chóng)項(xiàng)目douban
scrapy startproject douban
設(shè)置items.py文件,存儲(chǔ)要保存的數(shù)據(jù)類(lèi)型和字段名稱(chēng)
# -*- coding: utf-8 -*- import scrapy class DoubanItem(scrapy.Item): title = scrapy.Field() # 內(nèi)容 content = scrapy.Field() # 評(píng)分 rating_num = scrapy.Field() # 簡(jiǎn)介 quote = scrapy.Field()
設(shè)置爬蟲(chóng)文件doubanmovies.py
# -*- coding: utf-8 -*- import scrapy from douban.items import DoubanItem class DoubanmoviesSpider(scrapy.Spider): name = 'doubanmovies' allowed_domains = ['movie.douban.com'] offset = 0 url = 'https://movie.douban.com/top250?start=' start_urls = [url + str(offset)] def parse(self, response): # print('*'*60) # print(response.url) # print('*'*60) item = DoubanItem() info = response.xpath("http://div[@class='info']") for each in info: item['title'] = each.xpath(".//span[@class='title'][1]/text()").extract() item['content'] = each.xpath(".//div[@class='bd']/p[1]/text()").extract() item['rating_num'] = each.xpath(".//span[@class='rating_num']/text()").extract() item['quote'] = each .xpath(".//span[@class='inq']/text()").extract() yield item # print(item) self.offset += 25 if self.offset <= 250: yield scrapy.Request(self.url + str(self.offset),callback=self.parse)
設(shè)置管道文件,使用mongodb數(shù)據(jù)庫(kù)來(lái)保存爬取的數(shù)據(jù)。重點(diǎn)部分
# -*- coding: utf-8 -*- from scrapy.conf import settings import pymongo class DoubanPipeline(object): def __init__(self): self.host = settings['MONGODB_HOST'] self.port = settings['MONGODB_PORT'] def process_item(self, item, spider): # 創(chuàng)建mongodb客戶(hù)端連接對(duì)象,該例從settings.py文件里面獲取mongodb所在的主機(jī)和端口參數(shù),可直接書(shū)寫(xiě)主機(jī)和端口 self.client = pymongo.MongoClient(self.host,self.port) # 創(chuàng)建數(shù)據(jù)庫(kù)douban self.mydb = self.client['douban'] # 在數(shù)據(jù)庫(kù)douban里面創(chuàng)建表doubanmovies # 把類(lèi)似字典的數(shù)據(jù)轉(zhuǎn)換為phthon字典格式 content = dict(item) # 把數(shù)據(jù)添加到表里面 self.mysheetname.insert(content) return item
設(shè)置settings.py文件
# -*- coding: utf-8 -*- BOT_NAME = 'douban' SPIDER_MODULES = ['douban.spiders'] NEWSPIDER_MODULE = 'douban.spiders' USER_AGENT = 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0;' # Configure a delay for requests for the same website (default: 0) # See https://doc.scrapy.org/en/latest/topics/settings.html#download-delay # See also autothrottle settings and docs DOWNLOAD_DELAY = 3 # The download delay setting will honor only one of: #CONCURRENT_REQUESTS_PER_DOMAIN = 16 #CONCURRENT_REQUESTS_PER_IP = 16 # Disable cookies (enabled by default) COOKIES_ENABLED = False # Configure item pipelines # See https://doc.scrapy.org/en/latest/topics/item-pipeline.html ITEM_PIPELINES = { 'douban.pipelines.DoubanPipeline': 300, } # mongodb數(shù)據(jù)庫(kù)設(shè)置變量 MONGODB_HOST = '127.0.0.1' MONGODB_PORT = 27017
終端測(cè)試
scrapy crawl douban
這博客園的代碼片段縮進(jìn),難道要用4個(gè)空格才可以搞定?我發(fā)現(xiàn)只能使用4個(gè)空格才能解決如上圖的代碼塊的縮進(jìn)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python實(shí)現(xiàn)可獲取網(wǎng)易頁(yè)面所有文本信息的網(wǎng)易網(wǎng)絡(luò)爬蟲(chóng)功能示例
這篇文章主要介紹了Python實(shí)現(xiàn)可獲取網(wǎng)易頁(yè)面所有文本信息的網(wǎng)易網(wǎng)絡(luò)爬蟲(chóng)功能,涉及Python針對(duì)網(wǎng)頁(yè)的獲取、字符串正則判定等相關(guān)操作技巧,需要的朋友可以參考下2018-01-01解決python3 json數(shù)據(jù)包含中文的讀寫(xiě)問(wèn)題
今天小編就為大家分享一篇解決python3 json數(shù)據(jù)包含中文的讀寫(xiě)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-05-05python 使用遞歸的方式實(shí)現(xiàn)語(yǔ)義圖片分割功能
這篇文章主要介紹了python 使用遞歸的方式實(shí)現(xiàn)語(yǔ)義圖片分割,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07跟老齊學(xué)Python之總結(jié)參數(shù)的傳遞
這篇文章主要介紹了Python參數(shù)的傳遞的總結(jié),非常的實(shí)用,有需要的朋友可以參考下2014-10-10Python數(shù)據(jù)挖掘中常用的五種AutoEDA 工具總結(jié)
大家好,我們都知道在數(shù)據(jù)挖掘的過(guò)程中,數(shù)據(jù)探索性分析一直是非常耗時(shí)的一個(gè)環(huán)節(jié),但也是繞不開(kāi)的一個(gè)環(huán)節(jié),本篇文章帶你盤(pán)點(diǎn)數(shù)據(jù)挖掘中常見(jiàn)的5種 AutoEDA 工具2021-11-11Python調(diào)用Windows API函數(shù)編寫(xiě)錄音機(jī)和音樂(lè)播放器功能
這篇文章主要介紹了Python調(diào)用Windows API函數(shù)編寫(xiě)錄音機(jī)和音樂(lè)播放器功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-01-01