1

我正在尝试parse_page2为每个项目调用方法。但是每次我运行这个蜘蛛时,我每页只得到一个项目,所以我如何parse_page2为每个项目调用方法。

from scrapy.http import Request
from eScraper.items import EscraperItem
from scrapy.selector import HtmlXPathSelector
from scrapy.contrib.spiders import CrawlSpider

#------------------------------------------------------------------------------ 

class ESpider(CrawlSpider):
    name = "atisundarSpider"

    allowed_domains = ["atisundar.com"]

    URLSList = []

    for n in range (1,20):

        URLSList.append('http://atisundar.com/collections/sarees?page=' + str(n))
        URLSList.append('http://atisundar.com/collections/salwar-suits?page=' + str(n))

    start_urls = URLSList

    def parse(self, response):

        item = EscraperItem()
        hxs = HtmlXPathSelector(response)
        sites = hxs.select('//div[@class="block product size-medium"]')
        items = []

        for site in sites:
            item = EscraperItem()
            item['productDesc'] = "" 
            item['productSite'] = ["http://atisundar.com/"]
            item['productTitle'] = site.select('.//div[@class="main"]/a/@title').extract()
            item['productURL'] = ["http://atisundar.com" + site.select('.//div[@class="main"]/a/@href').extract()[0].encode('utf-8')]
            item['productPrice'] = site.select('.//p[@class="pricearea"]//span[@class="was-price"]/text()').extract() + site.select('.//p[@class="pricearea"]//span[@class="price"]/text()').extract() 
            item['productImage'] = [site.select('.//div[@class="main"]/a/img/@src').extract()[0].split('?')[0]] + [site.select('.//div[@class="main"]/a/img/@src').extract()[0].split('?')[0].replace("medium","grande")]
            item['image_urls'] = item['productImage']
            items.append(item)

            secondURL = "http://admin.atisundar.com/store/skuDetails?product_id=" + site.select('.//div[@class="main"]/a/text()').extract()[1].strip().split("#")[-1]

            request = Request(secondURL,
                      callback=self.parse_page2)
            request.meta['item'] = item 

            return request

    def parse_page2(self, response):

        item = response.meta['item']
        #item['other_url'] = response.url
        return item
4

1 回答 1

1

1)你没有使用CrawlSpider功能,我建议你继承你的蜘蛛BaseSpider

2) 在for循环中

for site in sites:

使用yieldthen return,否则它将在第一次迭代中打破循环。

yield request

3) 在 parse_page2 中item从而response.request.meta不是从response.meta

item = response.request.meta['item']

它现在应该可以工作了。

from scrapy.http import Request
from eScraper.items import EscraperItem
from scrapy.selector import HtmlXPathSelector

#------------------------------------------------------------------------------ 
from scrapy.spider import BaseSpider


class ESpider(BaseSpider):
    name = "atisundarSpider"

    allowed_domains = ["atisundar.com"]

    URLSList = []

    for n in range (1,20):

        URLSList.append('http://atisundar.com/collections/sarees?page=' + str(n))
        URLSList.append('http://atisundar.com/collections/salwar-suits?page=' + str(n))

    start_urls = URLSList

def parse(self, response):
    item = EscraperItem()
    hxs = HtmlXPathSelector(response)
    sites = hxs.select('//div[@class="block product size-medium"]')
    for site in sites:
        item = EscraperItem()
        item['productDesc'] = "" 
        item['productSite'] = ["http://atisundar.com/"]
        item['productTitle'] = site.select('.//div[@class="main"]/a/@title').extract()
        item['productURL'] = ["http://atisundar.com" + site.select('.//div[@class="main"]/a/@href').extract()[0].encode('utf-8')]
        item['productPrice'] = site.select('.//p[@class="pricearea"]//span[@class="was-price"]/text()').extract() + site.select('.//p[@class="pricearea"]//span[@class="price"]/text()').extract() 
        item['productImage'] = [site.select('.//div[@class="main"]/a/img/@src').extract()[0].split('?')[0]] + [site.select('.//div[@class="main"]/a/img/@src').extract()[0].split('?')[0].replace("medium","grande")]
        item['image_urls'] = item['productImage']
        secondURL = "http://admin.atisundar.com/store/skuDetails?product_id=" + site.select('.//div[@class="main"]/a/text()').extract()[1].strip().split("#")[-1]
        request = Request(secondURL,
                  callback=self.parse_page2)
        request.meta['item'] = item 
        yield request

def parse_page2(self, response):

    item = response.request.meta['item']
    #item['other_url'] = response.url
    return item
于 2013-05-28T14:06:41.783 回答