Python使用mongodb保存爬取豆瓣电影的数据过程解析

yipeiwu_com5年前Python爬虫

创建爬虫项目douban

scrapy startproject douban

设置items.py文件,存储要保存的数据类型和字段名称

# -*- coding: utf-8 -*-
import scrapy
class DoubanItem(scrapy.Item):
 title = scrapy.Field()
 # 内容
 content = scrapy.Field()
 # 评分
 rating_num = scrapy.Field()
 # 简介
 quote = scrapy.Field()

设置爬虫文件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("//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)

设置管道文件,使用mongodb数据库来保存爬取的数据。重点部分

# -*- 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):
  # 创建mongodb客户端连接对象,该例从settings.py文件里面获取mongodb所在的主机和端口参数,可直接书写主机和端口
  self.client = pymongo.MongoClient(self.host,self.port)
  # 创建数据库douban
  self.mydb = self.client['douban']
  # 在数据库douban里面创建表doubanmovies
  # 把类似字典的数据转换为phthon字典格式
  content = dict(item)
  # 把数据添加到表里面
  self.mysheetname.insert(content)
  return item

设置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数据库设置变量
MONGODB_HOST = '127.0.0.1'
MONGODB_PORT = 27017

终端测试

scrapy crawl douban

这博客园的代码片段缩进,难道要用4个空格才可以搞定?我发现只能使用4个空格才能解决如上图的代码块的缩进

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python的爬虫程序编写框架Scrapy入门学习教程

Python的爬虫程序编写框架Scrapy入门学习教程

1. Scrapy简介 Scrapy是一个为了爬取网站数据,提取结构性数据而编写的应用框架。 可以应用在包括数据挖掘,信息处理或存储历史数据等一系列的程序中。 其最初是为了页面抓取 (更...

Python网络爬虫之爬取微博热搜

Python网络爬虫之爬取微博热搜

微博热搜的爬取较为简单,我只是用了lxml和requests两个库 url= https://s.weibo.com/top/summary?Refer=top_hot&topnav=1...

Python3实战之爬虫抓取网易云音乐的热门评论

Python3实战之爬虫抓取网易云音乐的热门评论

前言 之前刚刚入门python爬虫,有大概半个月时间没有写python了,都快遗忘了。于是准备写个简单的爬虫练练手,我觉得网易云音乐最优特色的就是其精准的歌曲推荐和独具特色的用户评论,于...

python爬虫增加访问量的方法

看着自己少得可怜的访问量,突然有一个想用爬虫刷访问量的想法,主要也是抱着尝试的心态,学习学习。 其实市面上有一些软件可以代刷流量 比如 流量精灵,使用感确实比我们自己写的代码要好一些 第...

python爬虫 线程池创建并获取文件代码实例

本实例主要进行线程池创建,多线程获取、存储视频文件 梨视频:利用线程池进行视频爬取 #爬取梨视频数据 import requests import re from lxml impo...