このアドレスの「サイズ」セクションの javascript リストをスクレイピングしたい:
私がしたいのは、在庫のあるサイズを取得することです。リストが返されます。どうすればそれができますか?
私の完全なコードは次のとおりです。
# -*- coding: utf-8 -*-
from scrapy import Spider
from scrapy.http import Request
class ShoesSpider(Spider):
name = "shoes"
allowed_domains = ["store.nike.com"]
start_urls = ['http://store.nike.com/us/en_us/pd/magista-opus-ii-tech-craft-2-mens-firm-ground-soccer-cleat/pid-11229710/pgid-11918119']
def parse(self, response):
shoes = response.xpath('//*[@class="grid-item-image-wrapper sprite-sheet sprite-index-0"]/a/@href').extract()
for shoe in shoes:
yield Request(shoe, callback=self.parse_shoes)
def parse_shoes(self, response):
name = response.xpath('//*[@itemprop="name"]/text()').extract_first()
price = response.xpath('//*[@itemprop="price"]/text()').extract_first()
#sizes = ??
yield {
'name' : name,
'price' : price,
'sizes' : sizes
}
ありがとう
在庫のあるサイズを抽出するコードは次のとおりです。
import scrapy
class ShoesSpider(scrapy.Spider):
name = "shoes"
allowed_domains = ["store.nike.com"]
start_urls = ['http://store.nike.com/us/en_us/pd/magista-opus-ii-tech-craft-2-mens-firm-ground-soccer-cleat/pid-11229710/pgid-11918119']
def parse(self, response):
sizes = response.xpath('//*[@class="nsg-form--drop-down exp-pdp-size-dropdown exp-pdp-dropdown two-column-dropdown"]/option')
for s in sizes:
size = s.xpath('text()[not(parent::option/@class="exp-pdp-size-not-in-stock selectBox-disabled")]').extract_first('').strip()
yield{'Size':size}
結果は次のとおりです。
M 4 / W 5.5
M 4.5 / W 6
M 6.5 / W 8
M 7 / W 8.5
M 7.5 / 9 W
/ M 8 W 9.5
M 8.5 / W 10
M 9 / 10.5 W
for ループの中でこのように書くと、在庫の有無にかかわらず、すべてのサイズが抽出されます。
size = s.xpath('text()').extract_first('').strip()
ただし、在庫があるもののみを取得したい場合は、「exp-pdp-size-not-in-stock selectBox-disabled」クラスでマークされており、これを追加して除外する必要があります。
[not(parent::option/@class="exp-pdp-size-not-in-stock selectBox-disabled")]
他の靴のページでテストしましたが、同様に機能します。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加