[英]Scrapy doesn't recognise xpath
我嘗試從此頁面https://octopart.com/electronic-parts/integrated-circuits-ics獲取數據,但要從“規格”按鈕獲取數據。 我嘗試使用此代碼獲取產品名稱,但是它不起作用。
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']
正確的格式是在“類”之前添加“ @”
// DIV [@類= '內體'] /格[@類= 'SERP穿孔卷繞所有'] / ..
上面的鏈接中沒有“矩陣表”表。
嘗試使用類似:
// DIV [@類= '內體'] /格[@類= 'SERP穿孔卷繞所有'] // * [含有(@類, '矩陣表')]
如果只需要頂級產品名稱,請使用CSS選擇器
.serp-card-pdp-link
並提取文字
中位數價格來自CSS選擇器
.avg-price-faux-btn
您可以使用.css(selector)
將CSS應用於scrapy
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.