scrapy豆瓣top250

作者: Aedda | 来源:发表于2019-04-04 19:03 被阅读2次

怎么创建scrapy项目

  • init.py(无需修改)
  • douban_spider.py
# -*- coding: utf-8 -*-
import scrapy
from douban.items import DoubanItem
import re


class DoubanSpiderSpider(scrapy.Spider):
    # 爬虫名
    name = 'douban_spider'
    # 允许的域名
    allowed_domains = ['movie.douban.com']
    # 入口url
    start_urls = ['https://movie.douban.com/top250']

    #默认的请求方法
    def parse(self, response):
        #循环电影的条目
        movie_list = response.xpath('//div[@class="article"]//ol[@class="grid_view"]/li')
        for i_itme in movie_list:
            #倒入itme文件
            douban_itme = DoubanItem()
            #解析详情
            douban_itme['serial_number'] = i_itme.xpath('./div/div[1]/em/text()').extract_first()
            douban_itme['movie_name'] =i_itme.xpath('./div/div[2]/div[1]/a/span[1]/text()').extract_first()
            #若遇到不够规范的数据,进行清洗
            jieshao=i_itme.xpath('./div/div[2]/div[2]/p[1]/text()').extract()
            douban_itme['introduce'] = ''.join((''.join(jieshao)).split())
            douban_itme['star'] = i_itme.xpath('./div/div[2]/div[2]/div/span[2]/text()').extract_first()
            douban_itme['evaluate'] = i_itme.xpath('./div/div[2]/div[2]/div/span[4]/text()').extract_first()
            douban_itme['describe'] = i_itme.xpath('./div/div[2]/div[2]/p[2]/span/text()').extract_first()
            yield douban_itme
        #解析下一页规则,提取下一页的xpath
        next_link=response.xpath('//*[@id="content"]/div/div[1]/div[2]/span[3]/a/@href').extract()
        #如果下一页为真,把拼接后的url打回self.parse
        if next_link:
            next_link=next_link[0]
            yield scrapy.Request('https://movie.douban.com/top250'+next_link,callback=self.parse)
  • items.py
# -*- coding: utf-8 -*-

# Define here the models for your scraped items
#
# See documentation in:
# https://doc.scrapy.org/en/latest/topics/items.html

import scrapy


class DoubanItem(scrapy.Item):
    # define the fields for your item here like:
    # name = scrapy.Field()
    #序号
    serial_number=scrapy.Field()
    #电影名称
    movie_name=scrapy.Field()
    #电影介绍
    introduce=scrapy.Field()
    #星级
    star=scrapy.Field()
    #电影评论数
    evaluate=scrapy.Field()
    #电影描述
    describe=scrapy.Field()
  • main.py(自行创建)
from scrapy import cmdline

# 跑程序
cmdline.execute('scrapy crawl douban_spider'.split())

# 保存爬取的东西到json
#cmdline.execute('scrapy crawl douban_spider -o ../json/豆瓣top250.csv'.split())
  • middlewares.py(无需修改)
  • pipelines.py (无需修改)
  • settings.py
# -*- coding: utf-8 -*-

# Scrapy settings for douban project
#
# For simplicity, this file contains only settings considered important or
# commonly used. You can find more settings consulting the documentation:
#
#     https://doc.scrapy.org/en/latest/topics/settings.html
#     https://doc.scrapy.org/en/latest/topics/downloader-middleware.html
#     https://doc.scrapy.org/en/latest/topics/spider-middleware.html

BOT_NAME = 'douban'

SPIDER_MODULES = ['douban.spiders']
NEWSPIDER_MODULE = 'douban.spiders'


# Crawl responsibly by identifying yourself (and your website) on the user-agent
USER_AGENT = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36'

# Obey robots.txt rules
ROBOTSTXT_OBEY = False

# Configure maximum concurrent requests performed by Scrapy (default: 16)
#CONCURRENT_REQUESTS = 32

# 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 = 0.5
# 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

# Disable Telnet Console (enabled by default)
#TELNETCONSOLE_ENABLED = False

# Override the default request headers:
#DEFAULT_REQUEST_HEADERS = {
#   'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
#   'Accept-Language': 'en',
#}

# Enable or disable spider middlewares
# See https://doc.scrapy.org/en/latest/topics/spider-middleware.html
#SPIDER_MIDDLEWARES = {
#    'douban.middlewares.DoubanSpiderMiddleware': 543,
#}

# Enable or disable downloader middlewares
# See https://doc.scrapy.org/en/latest/topics/downloader-middleware.html
#DOWNLOADER_MIDDLEWARES = {
#    'douban.middlewares.DoubanDownloaderMiddleware': 543,
#}

# Enable or disable extensions
# See https://doc.scrapy.org/en/latest/topics/extensions.html
#EXTENSIONS = {
#    'scrapy.extensions.telnet.TelnetConsole': None,
#}

# Configure item pipelines
# See https://doc.scrapy.org/en/latest/topics/item-pipeline.html
#ITEM_PIPELINES = {
#    'douban.pipelines.DoubanPipeline': 300,
#}

# Enable and configure the AutoThrottle extension (disabled by default)
# See https://doc.scrapy.org/en/latest/topics/autothrottle.html
#AUTOTHROTTLE_ENABLED = True
# The initial download delay
#AUTOTHROTTLE_START_DELAY = 5
# The maximum download delay to be set in case of high latencies
#AUTOTHROTTLE_MAX_DELAY = 60
# The average number of requests Scrapy should be sending in parallel to
# each remote server
#AUTOTHROTTLE_TARGET_CONCURRENCY = 1.0
# Enable showing throttling stats for every response received:
#AUTOTHROTTLE_DEBUG = False

# Enable and configure HTTP caching (disabled by default)
# See https://doc.scrapy.org/en/latest/topics/downloader-middleware.html#httpcache-middleware-settings
#HTTPCACHE_ENABLED = True
#HTTPCACHE_EXPIRATION_SECS = 0
#HTTPCACHE_DIR = 'httpcache'
#HTTPCACHE_IGNORE_HTTP_CODES = []
#HTTPCACHE_STORAGE = 'scrapy.extensions.httpcache.FilesystemCacheStorage'

相关文章

网友评论

    本文标题:scrapy豆瓣top250

    本文链接:https://www.haomeiwen.com/subject/pdwqiqtx.html