Scrapy на обзорах TripAdvisor, некоторые элементы содержимого отзыва возвращаются пустыми - PullRequest
0 голосов
/ 15 мая 2019

Я исправил свой код для отзывов об отелях Scraping TripAdvisor из приведенного здесь вопроса и запустил его: Паук-скрап для Tripadvisor Прополз 0 страниц (при 0 страницах / мин) .Я перебираю отзывы об отелях по названию отеля, названию обзора и содержанию обзора.

При отладке одной страницы обзора на первой странице списка отелей (на первой странице 32 отеля и 5 отзывов на отель), я заметил две проблемы.

  1. Некоторые элементы содержимого отзыва не очищаются (возвращаются пустыми).Все названия отелей и названия возвращаются.
  2. Я бы ожидал, что всего 160 экспортируемых элементов будет экспортировано при выводе в файл json, но я получаю противоречивые числа (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'
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...