Scrapy无法识别xpath

问题描述 投票:0回答:2

我尝试从这个页面https://octopart.com/electronic-parts/integrated-circuits-ics获取数据,但是从Specs按钮。我尝试使用此代码获取产品的名称,但它不起作用。

class SpecSpider(scrapy.Spider):
name='specName'

start_urls = ['https://octopart.com/electronic-parts/integrated-circuits-ics']
custom_settings = {
    'DUPEFILTER_CLASS': 'scrapy.dupefilters.BaseDupeFilter',
}

def parse(self,response):

    return FormRequest.from_response(response, formxpath="//form[@class='btn-group']", clickdata={"value":"serp-grid"}, callback = self.scrape_pages)

def scrape_pages(self, response):
    #open_in_browser(response)
    items = SpecItem() 

    for product in response.xpath("//div[class='inner-body']/div[class='serp-wrap-all']/table[class='table-valign-middle matrix-table']"):

        name = product.xpath(".//tr/td[class='matrix-col-part']/a[class='nowrap']/text()").extract()            
        items['ProductName']=''.join(name).strip()

        price = product.xpath("//tr/td['4']/div[class='small']/text()").extract()
        items['Price'] = ''.join(price).strip()



        yield items

这个xpath response.xpath("//div[class='inner-body']/div[class='serp-wrap-all']/table[class='table-valign-middle matrix-table']")不起作用。

有什么建议

python xpath web-scraping scrapy
2个回答
1
投票

你使用错误的XPATH语法!

// div [class ='inner-body'] / div [class ='serp-wrap-all'] / table [class ='table-valign-middle matrix-table']

正确的格式是在“class”之前添加“@”

// DIV [@类= '内体'] /格[@类= 'SERP穿孔卷绕所有'] / ..

上面的链接中没有“矩阵表”表。

尝试使用类似的东西:

// DIV [@类= '内体'] /格[@类= 'SERP穿孔卷绕所有'] // * [含有(@类, '矩阵表')]


0
投票

如果您只想使用顶级产品名称,请使用css选择器

.serp-card-pdp-link

并提取文本

中位数价格来自css选择器

.avg-price-faux-btn

您可以使用.css(selector)将scss应用于scrapy

© www.soinside.com 2019 - 2024. All rights reserved.