Scrapy解析方法不起作用

时间:2013-05-17 09:06:19

标签: python xpath web-scraping scrapy scrapy-spider

我正在废弃website我在scrapy中写了一只蜘蛛,但我能用这个提取产品价格:

hxs.select('//div[@class="product_list"]//div[@class="product_list_offerprice"]/text()').extract()

通过scrapy shell

但是当我试图用蜘蛛做同样的事情时它会返回空列表

这是我的蜘蛛代码:

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

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

class ESpider(CrawlSpider):
    name = "ashikamallSpider"

    allowed_domains = ["ashikamall.com"]
    URLSList = []

    for n in range (1,51):
        URLSList.append('http://ashikamall.com/products.aspx?id=222&page=' + str(n))        

    start_urls = URLSList


    def parse(self, response):                  

        hxs = HtmlXPathSelector(response)        
        sites = hxs.select('//div[@class="product_list"]')        
        items = []

        for site in sites:
            item = EscraperItem()
            item['productDesc'] = ""            
            item['productSite'] = "http://1click1call.com/"
            item['productTitle'] =  site.select('div[@class="product_list_name"]/h3/text()').extract()
            item['productPrice'] = site.select('div[@class="product_list_offerprice"]/text()').extract()            
            item['productURL'] = "http://ashikamall.com/" + site.select('div[@class="product_list_image"]/a/@href').extract()[0].encode('utf-8')
            item['productImage'] = "http://ashikamall.com/" + site.select('div[@class="product_list_image"]/a/img/@src').extract()[0].encode('utf-8')
            items.append(item)
        return items

这是我的items.py

from scrapy.item import Item, Field

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

class EscraperItem(Item):

    image_urls = Field()    
    productURL = Field()
    productDesc = Field()
    image_paths = Field()
    productSite = Field()
    productTitle = Field()
    productPrice = Field()
    productImage = Field()

有人可以帮帮我吗?

1 个答案:

答案 0 :(得分:5)

问题在于你的xpath,它们应该是相对的(.//):

from scrapy.selector import HtmlXPathSelector
from scrapy.contrib.spiders import CrawlSpider

from scrapy.item import Item, Field


class EscraperItem(Item):
    image_urls = Field()
    productURL = Field()
    productDesc = Field()
    image_paths = Field()
    productSite = Field()
    productTitle = Field()
    productPrice = Field()
    productImage = Field()


class ESpider(CrawlSpider):
    name = "ashikamallSpider"

    allowed_domains = ["ashikamall.com"]
    start_urls = ['http://ashikamall.com/products.aspx?id=222&page=%s' % n for n in range(1, 51)]

    def parse(self, response):
        hxs = HtmlXPathSelector(response)
        sites = hxs.select('//div[@class="product_list"]')
        items = []

        for site in sites:
            item = EscraperItem()
            item['productDesc'] = ""
            item['productSite'] = "http://1click1call.com/"
            item['productTitle'] = site.select('.//div[@class="product_list_name"]/h3/text()').extract()
            item['productPrice'] = site.select('.//div[@class="product_list_offerprice"]/text()').extract()
            item['productURL'] = "http://ashikamall.com/" + site.select('.//div[@class="product_list_image"]/a/@href').extract()[0].encode('utf-8')
            item['productImage'] = "http://ashikamall.com/" + site.select('.//div[@class="product_list_image"]/a/img/@src').extract()[0].encode('utf-8')
            items.append(item)
        return items