python3_scrapy爬取腾讯视频“最新上架”影片信息

1.项目代码

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 TencentmovieItem(scrapy.Item):
    # define the fields for your item here like:
    # name = scrapy.Field()
    # 1.电影名称
    figure_title = scrapy.Field()

    # 2.主演
    main_performer = scrapy.Field()

    # 3.浏览量
    num = scrapy.Field()

    # 4.影片海报
    movie_img = scrapy.Field()

    # 5.影片摘要
    movie_abstract = scrapy.Field()

tencentmoive.py

# -*- coding: utf-8 -*-
import scrapy
from TencentMovie.items import TencentmovieItem


class TencentmoiveSpider(scrapy.Spider):
    name = 'tencentmoive'
    allowed_domains = ['v.qq.com']
    start_urls = ["https://v.qq.com/x/list/movie?sort=19&offset=0"]
    # baseURL = "https://v.qq.com/x/list/movie?sort=19&offset="
    # offset = 0

    baseURL = "https://v.qq.com/x/list/movie"

    def parse(self, response):
        node_list = response.xpath("//li[@class='list_item']")

        for node in node_list:
            item = TencentmovieItem()
            item["figure_title"] = node.xpath("./div/strong/a/text()").extract()[0]

            # 某些影片无主演
            if len(node.xpath("./div/a/text()")):
                item["main_performer"] = node.xpath("./div/a/text()").extract()[0]
            else:
                item["main_performer"] = "  "

            item["num"] = node.xpath("./div/span/text()").extract()[0]
            item["movie_img"] = node.xpath("./a/img/@src").extract()[0]

            # 某些影片无电影摘要
            if not len(node.xpath("./a/div/span/text()")):
                item["movie_abstract"] = "  "
            else:
                item["movie_abstract"] = node.xpath("./a/div/span/text()").extract()[0]

            yield item


        # self.offset += 30
        # if self.offset < 4980:
        #     url = self.baseURL+str(self.offset)
        #     yield scrapy.Request(url, callback=self.parse, dont_filter=True)

        # 非最后一页
        if not len(response.xpath("//a[@class='page_next disabled']")):
            url = response.xpath("//a[@class='page_next']/@href").extract()[0]
            yield scrapy.Request(self.baseURL+url, callback=self.parse, dont_filter=True)

pipelines.py

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

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


class TencentmoviePipeline(object):
    def __init__(self):
        self.f = open("tencentMoive.json", "w")

    def process_item(self, item, spider):
        content = json.dumps(dict(item), ensure_ascii=False) + ",\n"
        self.f.write(str(content.encode("utf-8"), encoding="utf-8"))
        return item

    def close_spider(self, spider):
        self.f.close()

settings.py

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

# Scrapy settings for TencentMovie 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 = 'TencentMovie'

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


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

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

# Enable or disable downloader middlewares
# See https://doc.scrapy.org/en/latest/topics/downloader-middleware.html
#DOWNLOADER_MIDDLEWARES = {
#    'TencentMovie.middlewares.TencentmovieDownloaderMiddleware': 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 = {
   'TencentMovie.pipelines.TencentmoviePipeline': 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'

start.py

# coding:utf-8
from scrapy import cmdline

cmdline.execute("scrapy crawl tencentmoive".split())

2.爬虫爬取结果部分展示

{"figure_title": "私事","main_performer": "Maisa Abd Elhadi", "num": "2669", "movie_img": "//i.gtimg.cn/qqlive/images/20150608/pic_v.png", "movie_abstract": "老夫老妻婚姻和谐指南"},
{"figure_title": "小羊快跑", "main_performer": "雷迪亚特·阿马雷", "num": "2170", "movie_img": "//i.gtimg.cn/qqlive/images/20150608/pic_v.png", "movie_abstract": "直击人心的最真实生活"},
{"figure_title": "埃贡·席勒:死神与少女", "main_performer": "诺亚·萨维德拉", "num": "9908", "movie_img": "//i.gtimg.cn/qqlive/images/20150608/pic_v.png", "movie_abstract": "最受争议的艺术家情史"},
{"figure_title": "出柙猛兽", "main_performer": "凯瑟琳·厄布", "num": "19万", "movie_img": "//i.gtimg.cn/qqlive/images/20150608/pic_v.png", "movie_abstract": "前囚犯救子心切"},
{"figure_title": "最终正义", "main_performer": "马克·达卡斯考斯", "num": "14万", "movie_img": "//i.gtimg.cn/qqlive/images/20150608/pic_v.png", "movie_abstract": "退役特种兵的绝地反击"},
{"figure_title": "很抱歉,如果我叫爱情", "main_performer": "克里斯蒂娜·布隆多", "num": "1538", "movie_img": "//i.gtimg.cn/qqlive/images/20150608/pic_v.png", "movie_abstract": "西班牙小清新电影"},
{"figure_title": "闪电巡逻队", "main_performer": "  ", "num": "2932", "movie_img": "//i.gtimg.cn/qqlive/images/20150608/pic_v.png", "movie_abstract": "南非警探逆袭成英雄"},
{"figure_title": "石之心", "main_performer": "莫里兹·布雷多", "num": "1万", "movie_img": "//i.gtimg.cn/qqlive/images/20150608/pic_v.png", "movie_abstract": "德国民间童话故事改编"},
{"figure_title": "我的妹妹是大明星", "main_performer": "Alice Foulcher", "num": "1万", "movie_img": "//i.gtimg.cn/qqlive/images/20150608/pic_v.png", "movie_abstract": "孪生姐妹花命运不同"},
{"figure_title": "椰子英雄", "main_performer": "Alex Ozerov", "num": "1万", "movie_img": "//i.gtimg.cn/qqlive/images/20150608/pic_v.png", "movie_abstract": "德国文艺小清新"},
{"figure_title": "相见恨晚", "main_performer": "  ", "num": "788", "movie_img": "//i.gtimg.cn/qqlive/images/20150608/pic_v.png", "movie_abstract": "笑着勇敢活下去"},
{"figure_title": "莫斯科不眠夜", "main_performer": "Yuriy Stoyanov", "num": "3389", "movie_img": "//i.gtimg.cn/qqlive/images/20150608/pic_v.png", "movie_abstract": "著名笑星最后的日子"},


猜你喜欢

转载自blog.csdn.net/admin_maxin/article/details/80060358