爬取4567电影网「建议收藏」

爬取4567电影网「建议收藏」movie.py虫子#-*-coding:utf-8-*-importscrapyfrommoviePro1.itemsimportMoviepro1ItemclassMovieS

大家好,又见面了,我是你们的朋友全栈君。

movie.py虫子

# -*- coding: utf-8 -*-
import scrapy
from moviePro1.items import Moviepro1Item


class MovieSpider(scrapy.Spider):
name = 'movie'
# allowed_domains = ['www.xxx.com']
start_urls = ['https://www.4567tv.tv/index.php/vod/show/class/喜剧/id/1.html']

# 定义通用爬虫模板:
url = "https://www.4567tv.tv/index.php/vod/show/class/喜剧/id/1/page/%d.html"

page = 1

# 用于解析电影名称:
def parse(self, response):
print("正在爬取第{}页的电影数据....".format(self.page))
li_list = response.xpath('/html/body/div[1]/div/div/div/div[2]/ul/li')
for li in li_list:
item = Moviepro1Item()
name = li.xpath('./div/a/@title').extract_first()
item["name"] = name
detail_url = 'https://www.4567tv.tv' + li.xpath('./div/a/@href').extract_first()

# 对详情页url手动请求发送
yield scrapy.Request(detail_url, callback=self.parse_detail,
meta={"item": item}) # 让Request将一个数据值(字典的形式)传递给回调函数
if self.page < 5:
print("-----------------------------------------------------------")
self.page += 1
new_url = format(self.url % self.page)
yield scrapy.Request(new_url, callback=self.parse)

# 解析电影简介:
def parse_detail(self, response):
# 接收请求传参的数据(字典的形式)
item = response.meta["item"]
desc = response.xpath('/html/body/div[1]/div/div/div/div[2]/p[5]/span[3]/text()').extract_first()
item["desc"] = desc
yield item

items.py

# -*- coding: utf-8 -*-

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

import scrapy


class Moviepro1Item(scrapy.Item):
# define the fields for your item here like:
name = scrapy.Field()
desc = scrapy.Field()

middlewares.py

# -*- coding: utf-8 -*-

# Define here the models for your spider middleware
#
# See documentation in:
# https://docs.scrapy.org/en/latest/topics/spider-middleware.html

from scrapy import signals


class Moviepro1SpiderMiddleware(object):
# Not all methods need to be defined. If a method is not defined,
# scrapy acts as if the spider middleware does not modify the
# passed objects.

@classmethod
def from_crawler(cls, crawler):
# This method is used by Scrapy to create your spiders.
s = cls()
crawler.signals.connect(s.spider_opened, signal=signals.spider_opened)
return s

def process_spider_input(self, response, spider):
# Called for each response that goes through the spider
# middleware and into the spider.

# Should return None or raise an exception.
return None

def process_spider_output(self, response, result, spider):
# Called with the results returned from the Spider, after
# it has processed the response.

# Must return an iterable of Request, dict or Item objects.
for i in result:
yield i

def process_spider_exception(self, response, exception, spider):
# Called when a spider or process_spider_input() method
# (from other spider middleware) raises an exception.

# Should return either None or an iterable of Request, dict
# or Item objects.
pass

def process_start_requests(self, start_requests, spider):
# Called with the start requests of the spider, and works
# similarly to the process_spider_output() method, except
# that it doesn’t have a response associated.

# Must return only requests (not items).
for r in start_requests:
yield r

def spider_opened(self, spider):
spider.logger.info('Spider opened: %s' % spider.name)


class Moviepro1DownloaderMiddleware(object):
# Not all methods need to be defined. If a method is not defined,
# scrapy acts as if the downloader middleware does not modify the
# passed objects.

@classmethod
def from_crawler(cls, crawler):
# This method is used by Scrapy to create your spiders.
s = cls()
crawler.signals.connect(s.spider_opened, signal=signals.spider_opened)
return s

def process_request(self, request, spider):
# Called for each request that goes through the downloader
# middleware.

# Must either:
# - return None: continue processing this request
# - or return a Response object
# - or return a Request object
# - or raise IgnoreRequest: process_exception() methods of
# installed downloader middleware will be called
return None

def process_response(self, request, response, spider):
# Called with the response returned from the downloader.

# Must either;
# - return a Response object
# - return a Request object
# - or raise IgnoreRequest
return response

def process_exception(self, request, exception, spider):
# Called when a download handler or a process_request()
# (from other downloader middleware) raises an exception.

# Must either:
# - return None: continue processing this exception
# - return a Response object: stops process_exception() chain
# - return a Request object: stops process_exception() chain
pass

def spider_opened(self, spider):
spider.logger.info('Spider opened: %s' % spider.name)

pipelines.py

# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


class Moviepro1Pipeline(object):
def process_item(self, item, spider):
print(item)
return item

settings.py

# -*- coding: utf-8 -*-

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

BOT_NAME = 'moviePro1'

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

USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36'



# Crawl responsibly by identifying yourself (and your website) on the user-agent
#USER_AGENT = 'moviePro1 (+http://www.yourdomain.com)'

# Obey robots.txt rules
ROBOTSTXT_OBEY = False
LOG_LEVEL = 'ERROR'

# 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://docs.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

# 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://docs.scrapy.org/en/latest/topics/spider-middleware.html
#SPIDER_MIDDLEWARES = {
# 'moviePro1.middlewares.Moviepro1SpiderMiddleware': 543,
#}

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

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

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

# Enable and configure the AutoThrottle extension (disabled by default)
# See https://docs.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://docs.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'
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/154556.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • IT速查手册[通俗易懂]

    IT速查手册[通俗易懂]发现一个很好的点评公司网站,很有创意。里面大部分的评论都来自公司内部的员工。  想找IT工作的,可以先到那里了解公司的待遇、环境、内部等问题。  公司点评网:

    2022年7月1日
    35
  • Android动态改变布局

    Android动态改变布局

    2021年9月4日
    53
  • 光纤交换机划ZONE

    光纤交换机划ZONE虽然我们在媒体上可以看到许多厂商声称有SAN交换机可以选择,其实这是一种假象,绝大多数厂商的SAN交换机都是OEM几个主要品牌的。目前在SAN交换机方面真正有实力主要有:IBM、Brocade(博科)、Cisco、McDATA等,像EMC这样的软件厂商基本上都是OEM其它厂商的SAN交换机产品。下图为Brocade(博科)交换机,本文也以其为例,记录其划分命令和划分方法:连接交换机…

    2022年5月11日
    69
  • 正则表达式中的特殊字符一览[通俗易懂]

    正则表达式中的特殊字符一览[通俗易懂]
    正则表达式中的特殊字符一览
     
    〓简介〓
    字符意义:对于字符,通常表示按字面意义,指出接着的字符为特殊字符,不作解释。
    例如:/b/匹配字符’b’,通过在b前面加一个反斜杠,也就是/b/,则该字符变成特殊字符,表示匹配一个单词的分界线。或者:对于几个字符,通常说明是特殊的,指出紧接着的字符不是特殊的,而应该按字面解释。例如:*是一个特殊字符,匹配任意个字符(包括0个字符);例如:/a*/意味匹配0个或多个a。为了匹配字面上的*,在a前面加一个反斜杠;

    2022年5月20日
    39
  • 自编码器原理概述_编码器结构及工作原理

    自编码器原理概述_编码器结构及工作原理个人博客:http://www.chenjianqu.com/原文链接:http://www.chenjianqu.com/show-62.html自编码器的概念自编码器(Auto-Encoder),是一种利用反向传播算法使得输出值等于输入值的神经网络,它先将输入压缩成潜在空间表征,然后通过这种表征来重构输出。自编码器由两部分组成:编码器:这部分能将输入压缩成潜在空…

    2022年10月1日
    4
  • SpringBoot中事务配置

    SpringBoot中事务配置SpringBoot创建的项目,默认没有事务,还是需要自己配,真是日了狗。还有那个启动类,对,就是包含main方法的那个类一定要放在包的最外层,最外层,最外层,不然有很多坑。包括但不限于不能扫描到你配置的类,连接ES时自定义接口无法自动注入等等。1.Xml方式跟Spring中差不多两步骤①.在resources文件夹下创建xml文件。例如:transaction.xml别问我为…

    2022年5月4日
    54

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号