我正在尝试执行一些图像抓取工具,该工具使用户能够使用 xpath 处理抓取的图像以查找哪些具有 alt 标签,哪些没有并将结果返回为两个给定页面中包含的所有图像单独的 json 对象
即{alted:["",""],nonAlted:["",""]}
现在我的问题来了,虽然我能够抓取页面并检索所有图像并将它们分成已更改和未更改的类别,但我无法将它们放入响应对象中!
我认为为了进一步阐明我的问题,最好添加一些代码,因此以下代码是我在 YQL 表的执行 block 中使用的代码:
query = "select * from html where url='http://www.example.com/page-path' and xpath='//li'";
var result = y.query(query);
y.log(result.results..img.(@alt));
var querieselement = <urls/>;
querieselement.query = result.results..img.(@alt);
response.object = querieselement;
所以我的问题是如何设置响应对象以包含已处理的图像列表,请注意,运行查询后结果不显示任何数据,尽管日志显示列表,希望有人能指出我导致该问题的原因。
P.S. 我在标题中提到“资源使用”的原因是因为我知道对每个图像类别执行单独调用的能力,这意味着我将同一页面抓取两次认为有点低效。
P.S. 如果有人能帮助我理解这两行的含义,我也很高兴
querieselement = <urls/>;
querieselement.query = result.results..img.(@alt);
为什么“
谢谢。
最佳答案
So my question is how can i set the response object to contain the processed list of the images
使用样式表而不是 XPath 选择器:
select * from xslt where url="http://www.mysite.com/page-path" and stylesheet="http://www.mysite.com/page-path.xsl"
这样定义样式表:
<xsl:template match="img[@alt]">
<xsl:for-each select="@alt">
<script>
alt.push(<xsl:value-of select="."/>);
</script>
</xsl:for-each>
</xsl:template>
<xsl:template match="img[not(@alt)]">
<xsl:for-each select="@src">
<script>
noalt.push(<xsl:value-of select="."/>);
</script>
</xsl:for-each>
</xsl:template>
关于javascript - 使用 YQL 以尽可能低的资源使用率(即最少的查询数量)执行图像抓取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13461474/