Python使用mongodb保存爬取豆瓣電影的數(shù)據(jù)過(guò)程解析
創(chuàng)建爬蟲項(xiàng)目douban
scrapy startproject douban
設(shè)置items.py文件,存儲(chǔ)要保存的數(shù)據(jù)類型和字段名稱
# -*- 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è)置爬蟲文件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客戶端連接對(duì)象,該例從settings.py文件里面獲取mongodb所在的主機(jī)和端口參數(shù),可直接書寫主機(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 # 把類似字典的數(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)文章
selenium.webdriver中add_argument方法常用參數(shù)表
這篇文章主要介紹了selenium.webdriver中add_argument方法常用參數(shù)表,需要的朋友可以參考下2021-04-04
Python?os.environ實(shí)戰(zhàn)應(yīng)用及技巧總結(jié)
這篇文章主要介紹了Python?os.environ實(shí)戰(zhàn)應(yīng)用及技巧的相關(guān)資料,os.environ是Python中管理環(huán)境變量的強(qiáng)大工具,提供了對(duì)系統(tǒng)環(huán)境變量的訪問和修改能力,需要的朋友可以參考下2025-03-03
使用matplotlib中scatter方法畫散點(diǎn)圖
這篇文章主要為大家詳細(xì)介紹了使用matplotlib中scatter方法畫散點(diǎn)圖,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-03-03
Python在后臺(tái)自動(dòng)解壓各種壓縮文件的實(shí)現(xiàn)方法
這篇文章主要介紹了Python在后臺(tái)自動(dòng)解壓各種壓縮文件的實(shí)現(xiàn)方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11
淺談python中統(tǒng)計(jì)計(jì)數(shù)的幾種方法和Counter詳解
今天小編就為大家分享一篇淺談python中統(tǒng)計(jì)計(jì)數(shù)的幾種方法和Counter詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11
在PYQT5中QscrollArea(滾動(dòng)條)的使用方法
今天小編就為大家分享一篇在PYQT5中QscrollArea(滾動(dòng)條)的使用方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-06-06
Selenium 模擬瀏覽器動(dòng)態(tài)加載頁(yè)面的實(shí)現(xiàn)方法
這篇文章主要介紹了Selenium 模擬瀏覽器動(dòng)態(tài)加載頁(yè)面的實(shí)現(xiàn)方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-05-05

