scrapy输出空csv而不发现错误

时间:2017-05-17 15:13:21

标签: python scrapy

因为我看到了

downloader/response_status_count/200': 2

我认为蜘蛛成功抓取了网页,但输出csv文件总是空的,有人知道发生了什么吗?非常感谢你!

我的蜘蛛代码就是这样,我检查了xpath,看起来是正确的:

from scrapy.spiders import Spider
from scrapy import Request
from jobs.items import BossItem


class BossSpider(Spider):
    name = 'Boss'
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36',
        'Referer': 'https://www.zhipin.com'}

    def start_requests(self):
        url = 'https://www.zhipin.com/c101020100/?query=%E4%BA%92%E8%81%94%E7%BD%91'
        yield Request(url, headers=self.headers)

    def parse(self, response):
        item = BossItem()
        job_fields = response.xpath('//div[@class="job-menu"]/dl')
        for jfield in job_fields:
            item['position'] = jfield.xpath('.//div[@class="info-primary"]/h3[@class="name"]/text()')[0]
            item['salary'] = jfield.xpath('.//div[@class="info-primary"]/h3[@class="name"]/span/text()')[0]
            item['location'] = jfield.xpath('.//div[@class="info-primary"]/p/text()')[0]
            item['experience'] = jfield.xpath('.//div[@class="info-primary"]/p/text()')[1]
            yield item

cmd信息如下:

(d:\Program Files\Anaconda3) C:\Users\Administrator\jobs>scrapy crawl Boss
 -o bs.csv
2017-05-17 23:02:57 [scrapy.utils.log] INFO: Scrapy 1.3.3 started (bot: jo
2017-05-17 23:02:57 [scrapy.utils.log] INFO: Overridden settings: {'SPIDER
ES': ['jobs.spiders'], 'FEED_FORMAT': 'csv', 'BOT_NAME': 'jobs', 'ROBOTSTX
': True, 'NEWSPIDER_MODULE': 'jobs.spiders', 'FEED_URI': 'bs.csv'}
2017-05-17 23:02:58 [scrapy.middleware] INFO: Enabled extensions:
['scrapy.extensions.corestats.CoreStats',
 'scrapy.extensions.logstats.LogStats',
 'scrapy.extensions.feedexport.FeedExporter',
 'scrapy.extensions.telnet.TelnetConsole']
2017-05-17 23:02:59 [scrapy.middleware] INFO: Enabled downloader middlewar
['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-05-17 23:02:59 [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-05-17 23:02:59 [scrapy.middleware] INFO: Enabled item pipelines:
[]
2017-05-17 23:02:59 [scrapy.core.engine] INFO: Spider opened
2017-05-17 23:02:59 [scrapy.extensions.logstats] INFO: Crawled 0 pages (at
es/min), scraped 0 items (at 0 items/min)
2017-05-17 23:02:59 [scrapy.extensions.telnet] DEBUG: Telnet console liste
n 127.0.0.1:6023
2017-05-17 23:03:00 [scrapy.core.engine] DEBUG: Crawled (200) <GET https:/
hipin.com/robots.txt> (referer: None)
2017-05-17 23:03:00 [scrapy.core.engine] DEBUG: Crawled (200) <GET https:/
hipin.com/c101020100/?query=%E4%BA%92%E8%81%94%E7%BD%91> (referer: https:/
hipin.com)
2017-05-17 23:03:01 [scrapy.core.engine] INFO: Closing spider (finished)
2017-05-17 23:03:01 [scrapy.statscollectors] INFO: Dumping Scrapy stats:
{'downloader/request_bytes': 593,
 'downloader/request_count': 2,
 'downloader/request_method_count/GET': 2,
 'downloader/response_bytes': 19695,
 'downloader/response_count': 2,
 'downloader/response_status_count/200': 2,
 'finish_reason': 'finished',
 'finish_time': datetime.datetime(2017, 5, 17, 15, 3, 1, 46342),
 'log_count/DEBUG': 3,
 'log_count/INFO': 7,
 'response_received_count': 2,
 'scheduler/dequeued': 1,
 'scheduler/dequeued/memory': 1,
 'scheduler/enqueued': 1,
 'scheduler/enqueued/memory': 1,
 'start_time': datetime.datetime(2017, 5, 17, 15, 2, 59, 786270)}
2017-05-17 23:03:01 [scrapy.core.engine] INFO: Spider closed (finished)

1 个答案:

答案 0 :(得分:-1)

job_fields中找不到任何物品,scrapy永远不会进入循环。所以它不会产生任何结果。