0

我只是第一次尝试填充一个项目,同时将它从一个页面传输到另一个页面。

它在每个循环中都有效,并且性别信息也在 parse_3 中正确到达,但 g2 不适合响应 url 的类别,并且 g1(第一类别级别)始终是我在 parse_sub 中循环遍历的列表中列表的最后一个元素...

当然我做错了什么,但我找不到问题,如果有人能解释我它是如何工作的,那就太好了。

最好的,杰克

class xspider(BaseSpider):
    name = 'x'
    allowed_domains = ['x.com']
    start_urls = ['http://www.x.com']

    def parse(self, response):
        hxs = HtmlXPathSelector(response)
        maincats = hxs.select('//ul[@class="Nav"]/li/a/@href').extract()[1:3]
        for maincat in maincats:
            item = catItem()
            if 'men' in maincat:
                item['gender'] = 'men'
                maincat = 'http://www.x.com' + maincat
                request = Request(maincat, callback=self.parse_sub)
                request.meta['item'] = item
            if 'woman' in maincat:
                item['gender'] = []
                item['gender'] = 'woman'
                maincat = 'http://www.x.com' + maincat
                request = Request(maincat, callback=self.parse_sub)
                request.meta['item'] = item
            yield request

    def parse_sub(self, response):
        i = 0
        hxs = HtmlXPathSelector(response)
        subcats = hxs.select('//ul[@class="sub Sprite"]/li/a/@href').extract()[0:5]
        text = hxs.select('//ul[@class="sub Sprite"]/li/a/span/text()').extract()[0:5]
        for item in text:
            item = response.meta['item']
            subcat = 'http://www.x.com' + subcats[i]
            request = Request(subcat, callback=self.parse_subcat)
            item['g1'] = text[i]
            item['gender'] = response.request.meta['item']
            i = i + 1
            request.meta['item'] = item
            yield request

    def parse_subcat(self, response):
        hxs = HtmlXPathSelector(response)
        test = hxs.select('//ul[@class="sub"]/li/a').extract()
        for s in test:
            item = response.meta['item']
            item['g2'] = []
            item['g2'] = hxs.select('//span[@class="Active Sprite"]/text()').extract()[0]
            s = s.encode('utf-8','ignore')
            link = s[s.find('href="')+6:][:s[s.find('href="')+6:].find('/"')]
            link = 'http://www.x.com/' + str(link) + '/'
            request = Request(link, callback=self.parse_3)
            request.meta['item'] = item
            yield request

    def parse_3(self, response):
        item = response.meta['item']
        print item
4

1 回答 1

2
def parse_subcat(self, response):
    hxs = HtmlXPathSelector(response)
    test = hxs.select('//ul[@class="sub"]/li/a').extract()
    for s in test:
        item = response.meta['item']
        item['g2'] = []
        item['g2'] = hxs.select('//span[@class="Active Sprite"]/text()').extract()[0]
        s = s.encode('utf-8','ignore')
        link = s[s.find('href="')+6:][:s[s.find('href="')+6:].find('/"')]
        link = 'http://www.x.com/' + str(link) + '/'
        request = Request(link, callback=self.parse_3)
        request.meta['item'] = item
        yield request

响应不包含元,但请求所以item = response.meta['item'] 应该是item = response.request.meta['item']

于 2013-02-11T09:46:18.210 回答