Python使用mongodb保存爬取豆瓣电影的数据过程解析
2019-08-14 10:51
821 查看
创建爬虫项目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的requests库爬取糗事百科并用xpath解析数据后存入MongoDB
- Python使用Beautiful Soup爬取豆瓣音乐排行榜过程解析
- Python爬虫(入门+进阶)学习笔记 1-5 使用pandas保存豆瓣短评数据
- #小练习 使用字典保存HTMLParser解析的数据 分类: python 小练习 HTMLParser 2013-11-08 20:51 574人阅读 评论(0) 收藏
- 使用Python解析豆瓣上Json格式数据
- 使用Python解析JSON数据的基本方法
- Android中使用xml保存数据(xml文件序列化)及使用pull解析xml文件
- 使用Python抓取豆瓣影评数据的方法
- python爬取网站数据保存使用的方法
- 使用Python解析JSON数据的基本方法
- python爬虫-爬取爱情公寓电影(2018)豆瓣短评并数据分析
- python爬虫学习(1):使用bs4爬豆瓣电影信息
- [151116 记录] 使用Python3.5爬取豆瓣电影Top250
- 使用Python3.5爬取豆瓣电影Top250
- Python 爬虫(以赛马数据为例)之使用BeautifulSoup进行Html解析
- python数据预处理并使用pickle模块保存
- python使用numpy读取、保存txt数据的实例
- 使用python对数据进行解析和读取——csv,json格式
- python2使用bs4爬取腾讯社招过程解析
- Python爬虫(入门+进阶)学习笔记 1-4 使用Xpath解析豆瓣短评