如何解决以下python-scrapy错误?

时间:2017-05-07 09:28:48

标签: python-2.7 scrapy

它在执行过程中没有显示错误但是我得到一个空白文件输出。我的蜘蛛代码如下:

   from scrapy.spider import BaseSpider
   from scrapy.selector import HtmlXPathSelector
   from example.items import exampleItem
   class MySpider(BaseSpider):
   name = "eg"
   allowed_domains = ["timeanddate.com"]
   start_urls = ["https://www.timeanddate.com/worldclock/"]
   def parse(self, response):
          titles = response.selector.xpath("/html/body/div[1]/div[8]/section[2]/div[1]/table/tbody")
     items = []
     for titles in titles:
        item = exampleItem()
        item["title"] = titles.xpath("//tr/td[@a]/text()").extract()
        item["link"] = titles.xpath("//tr/td[@class=rbi]").extract()
        items.append(item)
     return items

Scraper.item代码如下:

    from scrapy.item import Item, Field

    class exampleItem(Item):
    title = Field()
    link = Field()

LogFile输出如下,唯一显示的错误是: < 404 https://www.timeanddate.com/worldclock/)>:HTTP状态代码不是        处理或不允许

        [scrapy.utils.log] INFO: Scrapy 1.3.3 started (bot: example)
        [scrapy.utils.log] INFO: Overridden settings: {'NEWSPIDER_MODULE': 
        'example.spiders', 'FEED_URI': 'items.csv', 'SPIDER_MODULES': 
       ['example.spiders'], 'BOT_NAME': 'example', 'ROBOTSTXT_OBEY': True, 
        'FEED_FORMAT': 'csv'}
       [scrapy.middleware] INFO: Enabled extensions:
       ['scrapy.extensions.feedexport.FeedExporter',
       'scrapy.extensions.logstats.LogStats',
       'scrapy.extensions.telnet.TelnetConsole',
        'scrapy.extensions.corestats.CoreStats']
        [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']
    [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']
       [scrapy.middleware] INFO: Enabled item pipelines:
       []
       [scrapy.core.engine] INFO: Spider opened
       [scrapy.extensions.logstats] INFO: Crawled 0 pages (at 0 pages/min), 
       scraped 0 items (at 0 items/min)
       [scrapy.extensions.telnet] DEBUG: Telnet console listening on 
       127.0.0.1:6023
       [scrapy.core.engine] DEBUG: Crawled (200) <GET 
       https://www.timeanddate.com/robots.txt> (referer: None)
       [scrapy.core.engine] DEBUG: Crawled (404) <GET 
      https://www.timeanddate.com/worldclock/)> (referer: None)
   [scrapy.spidermiddlewares.httperror] INFO: Ignoring response 
   <404 https://www.timeanddate.com/worldclock/)>: HTTP status code is not 
   handled or not allowed
   [scrapy.core.engine] INFO: Closing spider (finished)
   [scrapy.statscollectors] INFO: Dumping Scrapy stats:
   {'downloader/request_bytes': 456,
    'downloader/request_count': 2,
    'downloader/request_method_count/GET': 2,
    'downloader/response_bytes': 6109,
     'downloader/response_count': 2,
   'downloader/response_status_count/200': 1,
   'downloader/response_status_count/404': 1,
    'finish_reason': 'finished',
   'finish_time': datetime.datetime(2017, 5, 7, 14, 2, 24, 993404),
      'log_count/DEBUG': 3,
    'log_count/INFO': 8,
    'response_received_count': 2,
    'scheduler/dequeued': 1,
    'scheduler/dequeued/memory': 1,
       'scheduler/enqueued': 1,
     'scheduler/enqueued/memory': 1,
    'start_time': datetime.datetime(2017, 5, 7, 14, 2, 23, 158763)}
     [scrapy.core.engine] INFO: Spider closed (finished)

1 个答案:

答案 0 :(得分:0)

原因是您的XPath selectors与网页来源不匹配。

webbrowser开发人员工具的一个已知问题是,它们显示响应数据中不存在此类元素的tbody元素。所以总是值得尝试不包括它们或绕过它们。

然后也存在一些代码问题。有一个for titles in titles:。对我来说看起来不对。

然后循环中的XPath语句也与我的开发人员工具不匹配。 //tr/td[@a]无法匹配。它应该是//tr/td[a]。但是后者一次匹配所有锚点,这使得提取单个项目变得困难。

所以我尝试重写你的代码并提出以下内容:

import scrapy

class MyItem(scrapy.Item):
    title = scrapy.Field()
    link = scrapy.Field()


class MySpider(scrapy.Spider):
    name = "eg"
    allowed_domains = ["timeanddate.com"]
    start_urls = ["https://www.timeanddate.com/worldclock/"]

    def parse(self, response):

        for sel_row in response.xpath('//table//tr'):
            for sel_td in sel_row.xpath('./td[a]'):

                item = MyItem()
                item["title"] = sel_td.xpath('./a/text()').extract_first()
                item["link"] = sel_td.xpath('./following::td[@class="rbi"][1]').extract_first()
                yield item

此代码使用scrapy 1.3.2 / Python 2.7.11进行测试。