scrapy response.xpath()导致内存泄漏

时间:2016-04-18 13:22:51

标签: xpath memory-leaks scrapy

我发现response.xpath()方法在使用scrapy编写蜘蛛时会泄漏内存。这是代码:

def extract_data(self, response):
    aomen_host_water = None
    aomen_pankou = None
    aomen_guest_water = None
    sb_host_water = None
    sb_pankou = None
    sb_guest_water = None


    # response.xpath('//div[@id="webmain"]/table[@id="odds"]/tr')
    # for tr in all_trs:
    #     # cname(company name)
    #     cname = tr.xpath('td[1]/text()').extract()
    #     if len(cname) == 0:
    #         continue
    #     # remove extra space and other stuff
    #     cname = cname[0].split(' ')[0]
    #     if cname == u'澳彩':
    #         aomen_host_water = tr.xpath('td[9]/text()').extract()
    #         if len(aomen_host_water) != 0:
    #             aomen_pankou = tr.xpath('td[10]/text()').extract()
    #             aomen_guest_water = tr.xpath('td[11]/text()').extract()
    #         else:
    #             aomen_host_water = tr.xpath('td[6]/text()').extract()
    #             aomen_pankou = tr.xpath('td[7]/text()').extract()
    #             aomen_guest_water = tr.xpath('td[8]/text()').extract()
    #     elif cname == u'SB':
    #         sb_host_water = tr.xpath('td[9]/text()').extract()
    #         if len(sb_host_water) != 0:
    #             sb_pankou = tr.xpath('td[10]/text()').extract()
    #             sb_guest_water = tr.xpath('td[11]/text()').extract()
    #         else:
    #             sb_host_water = tr.xpath('td[6]/text()').extract()
    #             sb_pankou = tr.xpath('td[7]/text()').extract()
    #             sb_guest_water = tr.xpath('td[8]/text()').extract()
    # if (aomen_host_water is None) or (aomen_pankou is None) or (aomen_guest_water is None) or \
    #         (sb_host_water is None) or (sb_pankou is None) or (sb_guest_water is None):
    #     return None
    # if (len(aomen_host_water) == 0) or (len(aomen_pankou) == 0) or (len(aomen_guest_water) == 0) or \
    #         (len(sb_host_water) == 0) or (len(sb_pankou) == 0) or (len(sb_guest_water) == 0):
    #     return None
    # item = YPItem()
    # item['aomen_host_water'] = float(aomen_host_water[0])
    # item['aomen_pankou'] = aomen_pankou[0].encode('utf-8')  # float(pankou.pankou2num(aomen_pankou[0]))
    # item['aomen_guest_water'] = float(aomen_guest_water[0])
    # item['sb_host_water'] = float(sb_host_water[0])
    # item['sb_pankou'] = sb_pankou[0].encode('utf-8') # float(pankou.pankou2num(sb_pankou[0]))
    # item['sb_guest_water'] = float(sb_guest_water[0])

    item = YPItem()
    item['aomen_host_water'] = 1.0
    item['aomen_pankou'] = '111'  # float(pankou.pankou2num(aomen_pankou[0]))
    item['aomen_guest_water'] = 1.0
    item['sb_host_water'] = 1.0
    item['sb_pankou'] = '111' # float(pankou.pankou2num(sb_pankou[0]))
    item['sb_guest_water'] = 1.0
    return item

这里我评论了有用的语句并使用了假数据,蜘蛛使用了大约45M内存,当我取消评论注释行时,蜘蛛使用100 + M内存并且内存使用量不断上升。有人在此之前遇到过这种问题吗?

1 个答案:

答案 0 :(得分:0)

您可以通过切换到extract_first()而不是extract()来减少内存使用量,这会产生不必要的列表。

我还会将scrapylxml升级到最新版本:

pip install --upgrade scrapy
pip install --upgrade lxml