爬取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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • visdom 使用教程

    visdom 使用教程visdom教程visdom安装与启动服务visdom常用功能image窗口:图像显示与更新窗口显示images窗口:多个图像显示与更新窗口显示text窗口:显示文本与更新文本line窗口:绘制折线图与更新折线图scatter窗口:绘制散点图与更新散点图visdom安装与启动服务安装visdompipinstallvisdom打开服务python-mvisdom.server…

    2022年6月24日
    32
  • java用户态和内核态「建议收藏」

    java用户态和内核态「建议收藏」在&lt;深入理解java虚拟机&gt;这本书上多次看到用户态和内核态两个名词,虽然大概能明白意思.但对于两者具体的定义和区别还是比较,特此查阅之后记录.内核态(KernelMode)与用户态(UserMode)内核态:CPU可以访问内存所有数据,包括外围设备,例如硬盘,网卡.CPU也可以将自己从一个程序切换到另一个程序用户态:只能受限的访问内存,且不允许访问外…

    2022年9月17日
    4
  • Quartus II 13.0sp1 (64-bit)使用教程「建议收藏」

    Quartus II 13.0sp1 (64-bit)使用教程「建议收藏」本人大三在学习计算机组成原理,要用到QuartusII13.0sp1(64-bit),但是下载安装完以后发现不会用,世界这么大,百度也没有任何收获,啊啊啊,昨天终于会用了,所以写了这个教程,希望对大家有用,详情见图片这里会弹出来一个框,然后(next)然后得到下面这个图这里也有一个(next)省略了,都是点一下哦然后(next)——》(finsh)module…

    2022年10月10日
    2
  • java中重定向和转发的区别_java中转发和重定向的使用区别

    java中重定向和转发的区别_java中转发和重定向的使用区别java重定向和转发的区别response.sendredirect("http://www.foo.com/path/error.html");重定向和转发有一个重要的不同:当使用转发时,JSP容器将使用一个内部的方法来调用目标页面,新的页面继续处理同一个请求,而浏览器将不会知道这个过程。与之相反,重定向方式的含义是第一个页面通知浏览器发送一个新的页面请求。因为,当你使用重定向时,浏览器中所…

    2025年8月29日
    10
  • pki密码技术_PKI体系管理

    pki密码技术_PKI体系管理HTTPS的诞生可先参考网络协议、HTTPS协议等文章明文传输对称加密“加密”和“解密”使用【相同的】密钥,如果密钥可以安全的传输,那么消息也应该可以安全的传输。非对称加密上述非对称加密与对称加密效果基本一样,如果公钥可以安全的传输,那么消息也应该可以安全的传输,接下来看看被劫持的情况。窃听者可以伪造服务器的公钥与客户端通讯,客户端以为是跟服务器通讯,其实是与窃听者在通讯。无论是对称加密还是非对称加密,都遗留了一个问题没有解决,那就是如何证明我们访问的网站就是我们

    2022年8月22日
    5
  • chmod 用法_举例说明chmod的两种用法

    chmod 用法_举例说明chmod的两种用法chmod[options]modefiles只能文件属主或特权用户才能使用该功能来改变文件存取模式。mode可以是数字形式或以whoopcodepermission形式表示。who是可选的,默认是a(所有用户)。只能选择一个opcode(操作码)。可指定多个mode,以逗号分开。options:-c,–changes只输出被改变文件的信息

    2022年10月20日
    2

发表回复

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

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