0

サイトhttp://www.jabong.com/Puma-Wirko-Ind-Black-Sneakers-187839.htmlからいくつかのコンテンツをスクレイピーを使用してスクレイピングしようとしています。以下のスクリプトは正常に実行されますが、一部の変数 (brand、mrp、pcode、pdesc) が null 値を取得することがあります。

 from scrapy.spider import BaseSpider
from scrapy.selector import HtmlXPathSelector
 from scrapy.http import Request
from tutorial.items import DmozItem




class DmozSpider(BaseSpider):
name = "dmozjabong"
allowed_domains = ["jabong.com"]
start_urls = [

 "http://www.jabong.com/Puma-Wirko-Ind-Black-Sneakers-187839.html"
]


def parse(self, response):
    hxs = HtmlXPathSelector(response)  
item = DmozItem()
item['title'] =' '.join(hxs.select('/html/head/title').extract()).strip()   
item['link'] = ' '.join(hxs.select('//*[@id="refurl"]/@value').extract()).strip()    
item['kwords'] = ' '.join(hxs.select('/html/head/meta[7]/@content').extract()).strip()  
item['mdes'] = ' '.join(hxs.select('/html/head/meta[6]/@content').extract()).strip()  
item['pname'] = ' '.join(hxs.select('//*[@id="qa-title-product"]/text()').extract()).strip()  
item['pcode'] = ' '.join(hxs.select('//div[@id="productInfo"]//table/tr[8]/td[2]/text()').extract()).strip()  
item['pdesc'] = ' '.join(hxs.select('//*[@id="productInfo"]/div[1]/div[2]/div[1]/p/text()').extract()).strip()  
item['pimg'] = hxs.select('//*[@id="wrapper"]/div[2]/div[1]/div[3]/div[1]/ul/li[1]/img/@src').extract()
item['brand'] = ' '.join(hxs.select('//*[@id="wrapper"]/div[2]/div[2]/div[1]/a/img/@alt').extract()).strip()
counter = 0
for image_data in item['pimg']:
        with open('image_' + str(counter) + '.jpg', 'wb') as fh:
            fh.write(image_data)
        counter += 1
item['bread'] = ' '.join(map(unicode.strip, hxs.select('id("breadcrumbs")//text()').extract())).strip()
item['listprice'] = ' '.join(hxs.select('//*[@id="before_price"]/span[2]/span/text()').extract()).strip()  
item['mrp'] = ' '.join(hxs.select('//*[@id="price_div"]/span[2]/strike/text()').extract()).strip()
print item
4

1 に答える 1

0

また、html コードにいくつかの小さな「間違い」またはコードのバリエーションがある可能性があるため、scrapy は何も返しません。見つけるための最良の方法は、機能する 1 つまたは 2 つのリンクと、空の情報を返す 1 つまたは 2 つのリンクを試してから、コードまたはスムスの違いを探すことです。

于 2013-10-17T09:11:42.613 に答える