对TripAdvisor的评论不满意,一些评论内容项为空

时间:2019-05-15 04:52:15

标签: python python-3.x web-scraping scrapy

我从这里的问题中修复了关于“刮擦TripAdvisor酒店评论”的代码,并将其运行:Scrapy spider for Tripadvisor Crawled 0 pages (at 0 pages/min)。我正在抓取酒店评论,以获得酒店名称,评论标题和评论内容。

在调试酒店列表第一页上的单个评论时(第一页中有32家酒店,每家酒店5条评论),我注意到了两个问题。

  1. 某些评论内容项没有被抓取(返回为空)。所有的酒店名称和标题都将返回。
  2. 当我输出到json文件时,我希望可以导出总共160个项目,但是我得到的数字不一致(一次140个,下一次150个,但是我没有期望的全部160个)

可能是什么问题?

我尝试插入自动油门和全局延迟,但这似乎无法解决问题。抓取似乎不需要任何等待就没有任何动态,但我感觉在解析的最低级别上正在发生问题。

这是我的Spider.py

import scrapy
from scrapy.spiders.crawl import CrawlSpider, Rule
from scrapy.linkextractors import LinkExtractor
from tripadvisor_reviews.items import TripadvisorReviewsItem


class tripadvisorSpider(scrapy.Spider):
    name = "tripadvisorspider"
    allowed_domains = ["tripadvisor.com"]
    start_urls = [
        "https://www.tripadvisor.com/Hotels-g147293-Punta_Cana_La_Altagracia_Province_Dominican_Republic-Hotels.html"
    ]

    rules = (
        Rule(LinkExtractor(allow=r'-\w+.html$'),
             callback='parse', follow=True),
    )

    def parse(self, response):
        for hotel in response.xpath('//div[contains(@class, "listing_title")]'):
            hotel_url = hotel.xpath('./a/@href').extract_first()
            hotel_name = hotel.xpath('./a/text()').extract()
            url = response.urljoin(hotel_url)
            yield scrapy.Request(url, callback=self.parse_hotel, meta={'hotel': hotel_name})

        # next_page = response.xpath(
        #     '//div[@class="unified ui_pagination standard_pagination"]/child::*[2][self::a]/@href')
        # if next_page:
        #     url = response.urljoin(next_page[0].extract())
        #     yield scrapy.Request(url, self.parse)

    def parse_hotel(self, response):
        for href in response.xpath('//a[contains(@class, "hotels-review-list-parts-ReviewTitle__reviewTitleText")]/@href'):
            url = response.urljoin(href.extract())
            hotel_name = response.meta['hotel']
            yield scrapy.Request(url, callback=self.parse_review, meta={'hotel': hotel_name})

        # next_page = response.xpath(
        #     '//div[@class="ui_pagination is-centered"]/child::*[2][self::a]/@href')
        # if next_page:
        #     url = response.urljoin(next_page[0].extract())
        #     yield scrapy.Request(url, self.parse_hotel)

    def parse_review(self, response):
        item = TripadvisorReviewsItem()
        item['hotel'] = response.meta["hotel"]
        item['title'] = response.xpath(
            '//h1[@class="title"]/text()').extract()
        item['content'] = response.xpath(
            '//span[@class="fullText "]/text()').extract()
        yield item

Settings.py

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

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

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


# Crawl responsibly by identifying yourself (and your website) on the user-agent
#USER_AGENT = 'tripadvisor_reviews (+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 = 2
# 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 = {
#    'tripadvisor_reviews.middlewares.TripadvisorReviewsSpiderMiddleware': 543,
# }

# Enable or disable downloader middlewares
# See https://doc.scrapy.org/en/latest/topics/downloader-middleware.html
DOWNLOADER_MIDDLEWARES = {
    #    'tripadvisor_reviews.middlewares.TripadvisorReviewsDownloaderMiddleware': 543,
    'scrapy.downloadermiddlewares.useragent.UserAgentMiddleware': None,
    'scrapy_useragents.downloadermiddlewares.useragents.UserAgentsMiddleware': 500,
}

USER_AGENTS = [
    ('Mozilla/5.0 (X11; Linux x86_64) '
     'AppleWebKit/537.36 (KHTML, like Gecko) '
     'Chrome/57.0.2987.110 '
     'Safari/537.36'),  # chrome
    ('Mozilla/5.0 (X11; Linux x86_64) '
     'AppleWebKit/537.36 (KHTML, like Gecko) '
     'Chrome/61.0.3163.79 '
     'Safari/537.36'),  # chrome
    ('Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:55.0) '
     'Gecko/20100101 '
     'Firefox/55.0'),  # firefox
    # ('Mozilla/5.0 (X11; Linux x86_64) '
    #  'AppleWebKit/537.36 (KHTML, like Gecko) '
    #  'Chrome/61.0.3163.91 '
    #  'Safari/537.36'),  # chrome
    # ('Mozilla/5.0 (X11; Linux x86_64) '
    #  'AppleWebKit/537.36 (KHTML, like Gecko) '
    #  'Chrome/62.0.3202.89 '
    #  'Safari/537.36'),  # chrome
    # ('Mozilla/5.0 (X11; Linux x86_64) '
    #  'AppleWebKit/537.36 (KHTML, like Gecko) '
    #  'Chrome/63.0.3239.108 '
    #  'Safari/537.36')  # chrom
]

# 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 = {
#    'tripadvisor_reviews.pipelines.TripadvisorReviewsPipeline': 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'

0 个答案:

没有答案