Python使用mongodb保存爬取豆瓣電影的數(shù)據(jù)過程解析
創(chuàng)建爬蟲項目douban
scrapy startproject douban
設置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()
設置爬蟲文件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)
設置管道文件,使用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
設置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ù)庫設置變量
MONGODB_HOST = '127.0.0.1'
MONGODB_PORT = 27017
終端測試
scrapy crawl douban
這博客園的代碼片段縮進,難道要用4個空格才可以搞定?我發(fā)現(xiàn)只能使用4個空格才能解決如上圖的代碼塊的縮進
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
selenium.webdriver中add_argument方法常用參數(shù)表
這篇文章主要介紹了selenium.webdriver中add_argument方法常用參數(shù)表,需要的朋友可以參考下2021-04-04
Python?os.environ實戰(zhàn)應用及技巧總結(jié)
這篇文章主要介紹了Python?os.environ實戰(zhàn)應用及技巧的相關(guān)資料,os.environ是Python中管理環(huán)境變量的強大工具,提供了對系統(tǒng)環(huán)境變量的訪問和修改能力,需要的朋友可以參考下2025-03-03
Python在后臺自動解壓各種壓縮文件的實現(xiàn)方法
這篇文章主要介紹了Python在后臺自動解壓各種壓縮文件的實現(xiàn)方法,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-11-11
淺談python中統(tǒng)計計數(shù)的幾種方法和Counter詳解
今天小編就為大家分享一篇淺談python中統(tǒng)計計數(shù)的幾種方法和Counter詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-11-11
Selenium 模擬瀏覽器動態(tài)加載頁面的實現(xiàn)方法
這篇文章主要介紹了Selenium 模擬瀏覽器動態(tài)加載頁面的實現(xiàn)方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-05-05

