Scrapy splash - 为什么我会获得URL重定向

时间:2017-10-02 08:16:45

标签: scrapy url-redirection scrapy-splash

我是scrapy和scrapy-splash的新手。我试图制作一个非常简单的脚本:获取网页的截图。不幸的是,我在抓取过程中得到了一个重定向,我得到的截图不是我想要的(网页上的订单是默认的,而不是我想要的)。

我尝试在我的启动请求中添加“splash_url = url”以强制网址,但后来我收到404错误(“HTTP状态代码未处理或不允许”)。

当我在浏览器上试用网址时(http:// ***:8050 /),我得到了预期的屏幕截图。

我做错了什么?

这是我的蜘蛛:

# -*- coding: utf-8 -*-
import scrapy
from scrapy_splash import SplashRequest

import base64
import os

class SplashtestSpider(scrapy.Spider):
    name = 'splashtest'
    allowed_domains = ['booking.com']
    start_urls = ['https://www.booking.com/searchresults.fr.html?city=1456928;dest_type=city;ss=paris;order=score']

    def parse(self, response):
        for url in self.start_urls: 
            yield SplashRequest(url,self.parse_info,endpoint='render.json',#splash_url=url,
                                args={'html':1,'png':1,'render_all':1,'wait':5})

def parse_info(self, response):

    path = os.getcwd()+"\\screenshot.png"
    png_bytes = base64.b64decode(response.data['png'])
    fichierpng = open(path,"wb")
    fichierpng.write(png_bytes)
    fichierpng.close()

这是我的设置文件:

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

# Scrapy settings for SplashTest project
#
# For simplicity, this file contains only settings considered important or
# commonly used. You can find more settings consulting the documentation:
#
#     http://doc.scrapy.org/en/latest/topics/settings.html
#     http://scrapy.readthedocs.org/en/latest/topics/downloader-middleware.html
#     http://scrapy.readthedocs.org/en/latest/topics/spider-middleware.html

BOT_NAME = 'SplashTest'

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

SPLASH_URL = 'http://192.168.99.100:8050/'

DOWNLOADER_MIDDLEWARES = {
    'scrapy_splash.SplashCookiesMiddleware': 723,
    'scrapy_splash.SplashMiddleware': 725,
    'scrapy.downloadermiddlewares.httpcompression.HttpCompressionMiddleware': 810,
}



SPIDER_MIDDLEWARES = {
    'scrapy_splash.SplashDeduplicateArgsMiddleware': 100,
}

DUPEFILTER_CLASS = 'scrapy_splash.SplashAwareDupeFilter'
HTTPCACHE_STORAGE = 'scrapy_splash.SplashAwareFSCacheStorage'

ROBOTSTXT_OBEY = True

# Configure maximum concurrent requests performed by Scrapy (default: 16)
CONCURRENT_REQUESTS = 1
CONCURRENT_REQUESTS_PER_DOMAIN = 1

# Enable and configure the AutoThrottle extension (disabled by default)
# See http://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

日志是:

2017-10-02 13:29:43 [scrapy.utils.log] INFO: Scrapy 1.4.0 started (bot: SplashTest)
2017-10-02 13:29:43 [scrapy.utils.log] INFO: Overridden settings: {'AUTOTHROTTLE_ENABLED': True, 'BOT_NAME': 'SplashTest', 'CONCURRENT_REQUESTS': 1, 'CONCURRENT_REQUESTS_PER_DOMAIN': 1, 'DUPEFILTER_CLASS': 'scrapy_splash.SplashAwareDupeFilter', 'HTTPCACHE_STORAGE': 'scrapy_splash.SplashAwareFSCacheStorage', 'LOG_FILE': 'log.txt', 'NEWSPIDER_MODULE': 'SplashTest.spiders', 'ROBOTSTXT_OBEY': True, 'SPIDER_MODULES': ['SplashTest.spiders']}
2017-10-02 13:29:43 [scrapy.middleware] INFO: Enabled extensions:
['scrapy.extensions.corestats.CoreStats',
 'scrapy.extensions.telnet.TelnetConsole',
 'scrapy.extensions.logstats.LogStats',
 'scrapy.extensions.throttle.AutoThrottle']
2017-10-02 13:29:44 [scrapy.middleware] INFO: Enabled downloader middlewares:
['scrapy.downloadermiddlewares.robotstxt.RobotsTxtMiddleware',
 'scrapy.downloadermiddlewares.httpauth.HttpAuthMiddleware',
 'scrapy.downloadermiddlewares.downloadtimeout.DownloadTimeoutMiddleware',
 'scrapy.downloadermiddlewares.defaultheaders.DefaultHeadersMiddleware',
 'scrapy.downloadermiddlewares.useragent.UserAgentMiddleware',
 'scrapy.downloadermiddlewares.retry.RetryMiddleware',
 'scrapy.downloadermiddlewares.redirect.MetaRefreshMiddleware',
 'scrapy.downloadermiddlewares.redirect.RedirectMiddleware',
 'scrapy.downloadermiddlewares.cookies.CookiesMiddleware',
 'scrapy_splash.SplashCookiesMiddleware',
 'scrapy_splash.SplashMiddleware',
 'scrapy.downloadermiddlewares.httpproxy.HttpProxyMiddleware',
 'scrapy.downloadermiddlewares.httpcompression.HttpCompressionMiddleware',
 'scrapy.downloadermiddlewares.stats.DownloaderStats']
2017-10-02 13:29:44 [scrapy.middleware] INFO: Enabled spider middlewares:
['scrapy.spidermiddlewares.httperror.HttpErrorMiddleware',
 'scrapy_splash.SplashDeduplicateArgsMiddleware',
 'scrapy.spidermiddlewares.offsite.OffsiteMiddleware',
 'scrapy.spidermiddlewares.referer.RefererMiddleware',
 'scrapy.spidermiddlewares.urllength.UrlLengthMiddleware',
 'scrapy.spidermiddlewares.depth.DepthMiddleware']
2017-10-02 13:29:44 [scrapy.middleware] INFO: Enabled item pipelines:
[]
2017-10-02 13:29:44 [scrapy.core.engine] INFO: Spider opened
2017-10-02 13:29:44 [scrapy.extensions.logstats] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
2017-10-02 13:29:44 [scrapy.extensions.telnet] DEBUG: Telnet console listening on 127.0.0.1:6023
2017-10-02 13:29:44 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://www.booking.com/robots.txt> (referer: None)
2017-10-02 13:29:51 [scrapy.downloadermiddlewares.redirect] DEBUG: Redirecting (301) to <GET https://www.booking.com/searchresults.fr.html?city=1456928;dest_type=city;ss=paris> from <GET https://www.booking.com/searchresults.fr.html?city=1456928;dest_type=city;ss=paris;order=score>
2017-10-02 13:29:56 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://www.booking.com/searchresults.fr.html?city=1456928;dest_type=city;ss=paris> (referer: None)
2017-10-02 13:29:57 [scrapy.core.engine] DEBUG: Crawled (404) <GET http://192.168.99.100:8050/robots.txt> (referer: None)
2017-10-02 13:30:06 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://www.booking.com/searchresults.fr.html?city=1456928;dest_type=city;ss=paris;order=score via http://192.168.99.100:8050/render.json> (referer: None)
2017-10-02 13:30:06 [scrapy.core.engine] INFO: Closing spider (finished)
2017-10-02 13:30:06 [scrapy.statscollectors] INFO: Dumping Scrapy stats:
{'downloader/request_bytes': 2202,
 'downloader/request_count': 5,
 'downloader/request_method_count/GET': 4,
 'downloader/request_method_count/POST': 1,
 'downloader/response_bytes': 3833046,
 'downloader/response_count': 5,
 'downloader/response_status_count/200': 3,
 'downloader/response_status_count/301': 1,
 'downloader/response_status_count/404': 1,
 'finish_reason': 'finished',
 'finish_time': datetime.datetime(2017, 10, 2, 11, 30, 6, 773931),
 'log_count/DEBUG': 6,
 'log_count/INFO': 7,
 'request_depth_max': 1,
 'response_received_count': 4,
 'scheduler/dequeued': 4,
 'scheduler/dequeued/memory': 4,
 'scheduler/enqueued': 4,
 'scheduler/enqueued/memory': 4,
 'splash/render.json/request_count': 1,
 'splash/render.json/response_count/200': 1,
 'start_time': datetime.datetime(2017, 10, 2, 11, 29, 44, 45545)}
2017-10-02 13:30:06 [scrapy.core.engine] INFO: Spider closed (finished)

0 个答案:

没有答案