我必须在此网站上抓取数据(名称、价格、描述、品牌...):https://www.asos.com/women/new-in/new-in-clothing/cat/?cid=2623&nlid=ww%7Cnew+in%7Cnew+products%7Cclothing
我的代码是这样的:
# -*- coding: utf-8 -*-
import scrapy
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider,Rule
class TestcrawlSpider(CrawlSpider):
name = 'testcrawl'
def remove_characters(self,value):
return value.strip('\n')
allowed_domains = ['www.asos.com']
start_urls = ['https://www.asos.com/women/new-in/new-in-clothing/cat/?cid=2623&nlid=ww|new+in|new+products|clothing']
rules = (
Rule(LinkExtractor(restrict_xpaths="//article[@class='_2qG85dG']/a"),callback='parse_item',follow=True),Rule(LinkExtractor(restrict_xpaths="//a[@class='_39_qNys']")),)
def parse_item(self,response):
yield{
'name':response.xpath("//div[@class='product-hero']/h1/text()").get(),'price':response.xpath("//span[@data-id='current-price']").get(),'description':response.xpath("//div[@class='product-description']/ul/li/text()").getall(),'about_me': response.xpath("//div[@class='about-me']//text()").getall(),'brand_description':response.xpath("//div[@class='brand-description']/p/text()").getall()
}
但是,由于 javascript,我无法获得价格。我需要通过 XHR 获得它。 我获取列表中只有一件商品的价格的代码如下:
import scrapy
import json
class Asosspider(scrapy.Spider):
name = 'asos'
allowed_domains = ['www.asos.com']
start_urls = ['https://www.asos.com/api/product/catalogue/v3/stockprice?productIds=200369183&store=ROW¤cy=GBP&keyStoreDataversion=hnm9sjt-28']
def parse(self,response):
#print(response.body)
resp = json.loads(response.text)[0]
price = resp.get('productPrice').get('current').get('text')
print(price)
yield {
'price': price
这里,我的 start_urls 是请求 URL。并且每一项都在不断变化。
只有 productsIds 发生了变化!!!
我需要在第一个代码中插入第二个代码才能获得价格吗?请问怎么做?
谢谢!