【发布时间】:2021-08-01 16:09:49
【问题描述】:
我必须在这个网站上抓取数据(名称、价格、描述、品牌...):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 正在改变!!!
我还需要在第一个代码中插入第二个代码以获取价格吗?请问怎么做?
谢谢!
【问题讨论】:
-
看起来
aria-labels 包含价格 - 你不能从那里拉价格吗?
标签: python selenium web-scraping xpath scrapy