【问题标题】:How to extract an item from a javascript event in scrapy?如何从scrapy中的javascript事件中提取项目?
【发布时间】:2018-08-10 15:22:46
【问题描述】:
import scrapy
from scrapy.linkextractors import LinkExtractor


class WoolRich(scrapy.Spider):
    name= "WoolRich_Spider"
    allowed_domains = ['woolrich.com']
    start_urls = ['https://www.woolrich.com/men/?sort=featured&page=1']

    def parse(self, response):
    links = response.css('li.product> article> figure> a::attr(href)').extract()
    for link in links:
        yield scrapy.Request(link,
                             callback=self.parse_of_individual_page)

    next_page=LinkExtractor(allow=[''], deny=['sort', 'size', 'Size', 'fsnf'])
    links = next_page.extract_links(response)
    for link in links:
        yield scrapy.Request(link.url,
                             callback=self.parse)

   # response.css('div.productView-image').extract()

def parse_of_individual_page(self, response):
    self.arbi = {
        'Product Name': response.css('h1.productView-title::text').extract(),
        'Style': response.css('.productView-product > div:nth-child(2) > strong:nth-child(1)::text').extract(),
        'Price': response.css('span.price::text')[0].extract(),
        'Size': response.css('span.form-option-variant::text').extract(),
        'Features': response.css('#features-content > li::text').extract(),
        'Description': response.css('#details-content::text').extract(),
        'Path from home': response.css('a.breadcrumb-label::text').extract(),
        'Image links': response.css('div.zoom> a::attr(data-zoom-image)').extract()



    }
    yield self.arbi

这是整个代码。我无法检索产品的颜色,因为它们是动态的。以这个 URl 为例:https://www.woolrich.com/mens-wool-stag-shirt-jac-6138/

它有多种颜色。只需要颜色的名称。

【问题讨论】:

  • 我不确定我是否理解您的问题,但您需要抓取仅在某些 Javascript 代码运行后才显示的页面数据。当您的 Scrapy 请求完成时,HTML 没有正确加载,对吧?我不知道这是否是最好的解决方案,但您可以使用像 Selenium 这样的 WebDriver,它基本上模仿了浏览器,然后您可以在页面完全加载后抓取页面。

标签: javascript python html scrapy


【解决方案1】:
response.css('label.form-option-swatch> span::attr(title)').extract()

这解决了问题。我在 HTML 中忽略了这一行

【讨论】:

    猜你喜欢
    • 2023-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-15
    • 2016-11-22
    • 2018-05-23
    • 1970-01-01
    相关资源
    最近更新 更多