Python使用mongodb保存爬取豆瓣電影的數(shù)據(jù)過程解析
創(chuàng)建爬蟲項目douban
scrapy startproject douban
設(shè)置items.py文件,存儲要保存的數(shù)據(jù)類型和字段名稱
# -*- coding: utf-8 -*- import scrapy class DoubanItem(scrapy.Item): title = scrapy.Field() # 內(nèi)容 content = scrapy.Field() # 評分 rating_num = scrapy.Field() # 簡介 quote = scrapy.Field()
設(shè)置爬蟲文件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ù)庫來保存爬取的數(shù)據(jù)。重點部分
# -*- 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客戶端連接對象,該例從settings.py文件里面獲取mongodb所在的主機和端口參數(shù),可直接書寫主機和端口 self.client = pymongo.MongoClient(self.host,self.port) # 創(chuàng)建數(shù)據(jù)庫douban self.mydb = self.client['douban'] # 在數(shù)據(jù)庫douban里面創(chuàng)建表doubanmovies # 把類似字典的數(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ù)庫設(shè)置變量 MONGODB_HOST = '127.0.0.1' MONGODB_PORT = 27017
終端測試
scrapy crawl douban
這博客園的代碼片段縮進,難道要用4個空格才可以搞定?我發(fā)現(xiàn)只能使用4個空格才能解決如上圖的代碼塊的縮進
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python實現(xiàn)可獲取網(wǎng)易頁面所有文本信息的網(wǎng)易網(wǎng)絡爬蟲功能示例
這篇文章主要介紹了Python實現(xiàn)可獲取網(wǎng)易頁面所有文本信息的網(wǎng)易網(wǎng)絡爬蟲功能,涉及Python針對網(wǎng)頁的獲取、字符串正則判定等相關(guān)操作技巧,需要的朋友可以參考下2018-01-01解決python3 json數(shù)據(jù)包含中文的讀寫問題
今天小編就為大家分享一篇解決python3 json數(shù)據(jù)包含中文的讀寫問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-05-05python 使用遞歸的方式實現(xiàn)語義圖片分割功能
這篇文章主要介紹了python 使用遞歸的方式實現(xiàn)語義圖片分割,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-07-07Python數(shù)據(jù)挖掘中常用的五種AutoEDA 工具總結(jié)
大家好,我們都知道在數(shù)據(jù)挖掘的過程中,數(shù)據(jù)探索性分析一直是非常耗時的一個環(huán)節(jié),但也是繞不開的一個環(huán)節(jié),本篇文章帶你盤點數(shù)據(jù)挖掘中常見的5種 AutoEDA 工具2021-11-11Python調(diào)用Windows API函數(shù)編寫錄音機和音樂播放器功能
這篇文章主要介紹了Python調(diào)用Windows API函數(shù)編寫錄音機和音樂播放器功能,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2020-01-01