我尝试从这个页面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']")
不起作用。
有什么建议
你使用错误的XPATH语法!
// div [class ='inner-body'] / div [class ='serp-wrap-all'] / table [class ='table-valign-middle matrix-table']
正确的格式是在“class”之前添加“@”
// DIV [@类= '内体'] /格[@类= 'SERP穿孔卷绕所有'] / ..
上面的链接中没有“矩阵表”表。
尝试使用类似的东西:
// DIV [@类= '内体'] /格[@类= 'SERP穿孔卷绕所有'] // * [含有(@类, '矩阵表')]
如果您只想使用顶级产品名称,请使用css选择器
.serp-card-pdp-link
并提取文本
中位数价格来自css选择器
.avg-price-faux-btn
您可以使用.css(selector)
将scss应用于scrapy