1. Project code
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. Movie name figure_title = scrapy.Field() # 2. Starring main_performer = scrapy.Field() # 3. Views num = scrapy.Field() # 4. Movie Poster movie_img = scrapy.Field() # 5. Video Summary 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] # Some movies do not have lead actors 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] # Some videos have no movie summary 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) # Not the last page 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. Partial display of crawler results
{"figure_title": "私事","main_performer": "Maisa Abd Elhadi", "num": "2669", "movie_img": "//i.gtimg.cn/qqlive/images/20150608/pic_v.png", "movie_abstract": "老夫老妻婚姻和谐指南"}, {"figure_title": "Lamb Run", "main_performer": "Rediyat Amare", "num": "2170", "movie_img": "//i.gtimg.cn/qqlive/ images/20150608/pic_v.png", "movie_abstract": "The most real life that hits the heart"}, {"figure_title": "Egon Schiller: Death and the Maiden", "main_performer": "Noah Saavedra", "num": "9908", "movie_img": "//i.gtimg. cn/qqlive/images/20150608/pic_v.png", "movie_abstract": "The most controversial artist love story"}, {"figure_title": "Beasts", "main_performer": "Catherine Erbe", "num": "190,000", "movie_img": "//i.gtimg.cn/qqlive/images/20150608/ pic_v.png", "movie_abstract": "Ex-prisoner is eager to save his son"}, {"figure_title": "Final Justice", "main_performer": "Mark Dacascos", "num": "140,000", "movie_img": "//i.gtimg.cn/qqlive/images/ 20150608/pic_v.png", "movie_abstract": "Retired Special Forces' Jedi Counterattack"}, {"figure_title": "Sorry if my name is love", "main_performer": "Christina Blondo", "num": "1538", "movie_img": "//i.gtimg.cn/qqlive /images/20150608/pic_v.png", "movie_abstract": "Spanish small fresh movie"}, {"figure_title": "Lightning Patrol", "main_performer": " ", "num": "2932", "movie_img": "//i.gtimg.cn/qqlive/images/20150608/pic_v.png", "movie_abstract": "South African police detectives become heroes"}, {"figure_title": "Heart of Stone", "main_performer": "Moritz Bredo", "num": "10,000", "movie_img": "//i.gtimg.cn/qqlive/images/ 20150608/pic_v.png", "movie_abstract": "Adaptation of German folk fairy tale"}, {"figure_title": "My sister is a big star", "main_performer": "Alice Foulcher", "num": "10,000", "movie_img": "//i.gtimg.cn/qqlive/images/20150608 /pic_v.png", "movie_abstract": "Twin sisters have different fates"}, {"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": "Sleepless in Moscow", "main_performer": "Yuriy Stoyanov", "num": "3389", "movie_img": "//i.gtimg.cn/qqlive/images/20150608/pic_v.png ", "movie_abstract": "The Last Days of the Famous Comedian"},