Scrapy:504网关超时错误

时间:2017-01-31 04:25:05

标签: python python-2.7 csv scrapy-spider

我是爬行领域的新手刮,我尝试使用这个代码,我上网开始:

items.py

import scrapy

class IkeaItem(scrapy.Item):
    name = scrapy.Field()
    link = scrapy.Field()

spider.py

import  scrapy
from ikea.items import IkeaItem

class IkeaSpider(scrapy.Spider):
    name = 'ikea'

    allowed_domains = ['http://www.ikea.com/']
    start_urls = ['http://www.ikea.com/']

    def parse(self, response):
        for sel in response.xpath('//tr/td/a'):
            item = IkeaItem()
            item['name'] = sel.xpath('text()').extract()
            item['link'] = sel.xpath('@href').extract()

            yield item

我运行以下命令将输出存储在csv文件中: scrapy crawl ikea -o items.csv -t csv 但是我得到了504网关超时错误,这使我处于修复状态该代码已被证实是一个有效的代码。

这是我得到的输出:

2017-01-31 09:36:36 [scrapy.utils.log] INFO: Scrapy 1.3.0 started (bot: ikea)
2017-01-31 09:36:36 [scrapy.utils.log] INFO: Overridden settings: {'NEWSPIDER_MODULE': 'ikea.spiders', 'FEED_URI': 'items.csv', 'SPIDER_MODULES': ['ikea.spiders'], 'BOT_NAME': 'ikea', 'ROBOTSTXT_OBEY': True, 'FEED_FORMAT': 'csv'}
2017-01-31 09:36:36 [scrapy.middleware] INFO: Enabled extensions:
['scrapy.extensions.feedexport.FeedExporter',
 'scrapy.extensions.logstats.LogStats',
 'scrapy.extensions.telnet.TelnetConsole',
 'scrapy.extensions.corestats.CoreStats']
2017-01-31 09:36:36 [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.httpcompression.HttpCompressionMiddleware',
 'scrapy.downloadermiddlewares.redirect.RedirectMiddleware',
 'scrapy.downloadermiddlewares.cookies.CookiesMiddleware',
 'scrapy.downloadermiddlewares.stats.DownloaderStats']
2017-01-31 09:36:36 [scrapy.middleware] INFO: Enabled spider middlewares:
['scrapy.spidermiddlewares.httperror.HttpErrorMiddleware',
 'scrapy.spidermiddlewares.offsite.OffsiteMiddleware',
 'scrapy.spidermiddlewares.referer.RefererMiddleware',
 'scrapy.spidermiddlewares.urllength.UrlLengthMiddleware',
 'scrapy.spidermiddlewares.depth.DepthMiddleware']
2017-01-31 09:36:36 [scrapy.middleware] INFO: Enabled item pipelines:
[]
2017-01-31 09:36:36 [scrapy.core.engine] INFO: Spider opened
2017-01-31 09:36:36 [scrapy.extensions.logstats] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
2017-01-31 09:36:36 [scrapy.extensions.telnet] DEBUG: Telnet console listening on 127.0.0.1:6023
2017-01-31 09:37:36 [scrapy.extensions.logstats] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
2017-01-31 09:37:37 [scrapy.downloadermiddlewares.retry] DEBUG: Retrying <GET http://www.ikea.com/robots.txt> (failed 1 times): 504 Gateway Time-out
2017-01-31 09:38:36 [scrapy.extensions.logstats] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
2017-01-31 09:38:38 [scrapy.downloadermiddlewares.retry] DEBUG: Retrying <GET http://www.ikea.com/robots.txt> (failed 2 times): 504 Gateway Time-out
2017-01-31 09:39:36 [scrapy.extensions.logstats] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
2017-01-31 09:39:39 [scrapy.downloadermiddlewares.retry] DEBUG: Gave up retrying <GET http://www.ikea.com/robots.txt> (failed 3 times): 504 Gateway Time-out
2017-01-31 09:39:39 [scrapy.core.engine] DEBUG: Crawled (504) <GET http://www.ikea.com/robots.txt> (referer: None)
2017-01-31 09:39:39 [scrapy.core.engine] DEBUG: Crawled (200) <GET http://www.ikea.com/> (referer: None)
2017-01-31 09:39:40 [scrapy.core.engine] INFO: Closing spider (finished)
2017-01-31 09:39:40 [scrapy.statscollectors] INFO: Dumping Scrapy stats:
{'downloader/request_bytes': 870,
 'downloader/request_count': 4,
 'downloader/request_method_count/GET': 4,
 'downloader/response_bytes': 10429,
 'downloader/response_count': 4,
 'downloader/response_status_count/200': 1,
 'downloader/response_status_count/504': 3,
 'finish_reason': 'finished',
 'finish_time': datetime.datetime(2017, 1, 31, 4, 9, 40, 52064),
 'log_count/DEBUG': 6,
 'log_count/INFO': 10,
 'response_received_count': 2,
 'scheduler/dequeued': 1,
 'scheduler/dequeued/memory': 1,
 'scheduler/enqueued': 1,
 'scheduler/enqueued/memory': 1,
 'start_time': datetime.datetime(2017, 1, 31, 4, 6, 36, 757725)}
2017-01-31 09:39:40 [scrapy.core.engine] INFO: Spider closed (finished) 

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

我已经测试了你的代码并且工作正常,我得到了200响应,但是0数据被废弃,因为你的选择器没有任何好处。

当时网页的服务器已关闭,这就是原因,现在还可以。

无论如何,你可以试试这个:

import urllib2
hdr = {"User-Agent": "My Agent"}

while True:

  try:
    req = urllib2.Request("http://www.reddit.com/", headers=hdr)
    response = urllib2.urlopen(req)
    htmlSource = response.read()
    if response.getcode() == 200:
        break
  except Exception as inst:
    print inst

此代码将尝试请求网页,直到它从网站服务器获得200响应。