使用ImportXML抓取Etsy自定义收藏夹名称失败求助
解决Google Sheets IMPORTXML抓取Etsy自定义收藏夹名称的问题
问题概述
使用IMPORTXML抓取Etsy店铺的自定义收藏夹名称时,精准定位的公式返回Imported content empty;简化XPath的公式仅能抓取系统自动生成的收藏夹名称,无法获取自定义名称。
原使用公式
复杂定位公式(返回空)
=index(IMPORTXML("https://www.etsy.com/people/413dgiv4?ref=hdr-fav&tab=items","div[@class='tabbed-nav-container wt-action-group wt-action-group--image fit-width-to-content wt-list-inline wt-flex-nowrap wt-overflow-y-hidden container-spacing wt-align-items-flex-start']/a[position()>2]/span[@class='wt-p-xs-0 wt-m-xs-0 nav-group-item nav-profile data-tab-title=']/div[@class='wt-mb-xs-2 wt-display-flex-xs wt-flex-direction-column-xs wt-align-items-center tab-details-container']/div[@class='wt-display-flex-xs wt-align-items-flex-start wt-text-center-xs']//p[@class='wt-text-caption-title wt-text-truncate--multi-line wt-break-word tab-title']"),1)
简化公式(仅能抓系统默认名称)
=IMPORTXML("https://www.etsy.com/people/413dgiv4?ref=hdr-fav&tab=items","//div[@class='tabbed-nav-container wt-action-group wt-action-group--image fit-width-to-content wt-list-inline wt-flex-nowrap wt-overflow-y-hidden container-spacing wt-align-items-flex-start']/a/span/div/div/p")
问题原因与解决方案
1. 修复XPath属性匹配错误
原复杂公式中span[@class='wt-p-xs-0 wt-m-xs-0 nav-group-item nav-profile data-tab-title=']存在写法错误:data-tab-title=是无效的属性定义,该属性实际带有具体值,不应写成空值形式。调整XPath,去掉错误片段后重新尝试:
=IMPORTXML("https://www.etsy.com/people/413dgiv4?ref=hdr-fav&tab=items","//div[@class='tabbed-nav-container wt-action-group wt-action-group--image fit-width-to-content wt-list-inline wt-flex-nowrap wt-overflow-y-hidden container-spacing wt-align-items-flex-start']/a[position()>2]//p[@class='wt-text-caption-title wt-text-truncate--multi-line wt-break-word tab-title']")
2. 解决动态内容加载限制
如果调整XPath后仍无法获取自定义收藏夹,大概率是因为Etsy的自定义收藏夹内容通过JavaScript动态渲染,而IMPORTXML仅能抓取页面初始静态HTML。此时可使用Google Apps Script模拟请求获取完整内容:
function getEtsyFavorites() { const url = "https://www.etsy.com/people/413dgiv4?ref=hdr-fav&tab=items"; const html = UrlFetchApp.fetch(url).getContentText(); const doc = XmlService.parse(html); // 定位收藏夹名称节点 const titles = doc.getDescendants() .filter(node => node.getType() == XmlService.NodeType.ELEMENT) .filter(element => element.getAttribute("class")?.getValue() === "wt-text-caption-title wt-text-truncate--multi-line wt-break-word tab-title") .map(element => element.getText()); // 将结果写入当前工作表A列 const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); sheet.getRange(1, 1, titles.length, 1).setValues(titles.map(title => [title])); }
使用方法:打开Google Sheets,点击「扩展程序」→「Apps脚本」,粘贴代码后保存运行,授权权限即可获取完整收藏夹列表。
3. 筛选排除系统默认收藏夹
若需要过滤掉"Items""Favorites"这类系统默认名称,可结合FILTER和REGEXMATCH实现:
=FILTER(IMPORTXML("https://www.etsy.com/people/413dgiv4?ref=hdr-fav&tab=items","//div[@class='tabbed-nav-container wt-action-group wt-action-group--image fit-width-to-content wt-list-inline wt-flex-nowrap wt-overflow-y-hidden container-spacing wt-align-items-flex-start']/a//p[@class='wt-text-caption-title wt-text-truncate--multi-line wt-break-word tab-title']"),NOT(REGEXMATCH(IMPORTXML("https://www.etsy.com/people/413dgiv4?ref=hdr-fav&tab=items","//div[@class='tabbed-nav-container wt-action-group wt-action-group--image fit-width-to-content wt-list-inline wt-flex-nowrap wt-overflow-y-hidden container-spacing wt-align-items-flex-start']/a//p[@class='wt-text-caption-title wt-text-truncate--multi-line wt-break-word tab-title']","Items|Favorites"))))
内容的提问来源于stack exchange,提问作者thc
相关产品推荐
相关产品推荐

