Scrapy不认识xpath

2024-06-16 12:28:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图从这个页面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

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

有什么建议吗


Tags: namehttpsselfdivcomresponsetableitems
2条回答

如果您只需要顶级产品名称,请使用

.serp-card-pdp-link

并提取文本

中间价来自css选择器

.avg-price-faux-btn

您可以使用.css(selector)将css与scrapy一起应用

您使用了错误的XPATH语法!你知道吗

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

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

//div[@class='inner-body']/div[@class='serp-wrap-all']/..

在上面的链接中没有“矩阵表”表。你知道吗

尝试使用以下方法:

//div[@class='inner-body']/div[@class='serp-wrap-all']//*[contains(@class,'matrix-table')]

相关问题 更多 >