【发布时间】: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