javascript - 如何从嵌套 div 标签中抓取数据并从开发工具导出到 CSV

标签 javascript google-chrome-devtools scrape

我正在尝试从 here 导出 div 标签“prod-details”我想知道是否有一种方法或脚本可以在 chrome 开发工具中运行来导出 CSV 并抓取数据?

最佳答案

Kayce 不为您执行此操作可能是正确的,但是开始可能会很困难,因此这里有一个基本的实现,其中包含您可以使用的注释。请务必阅读并尝试理解它,而不仅仅是复制和粘贴它。

// Create a list of the selectors within each prod-deatils you'd like to export as a column
var columnSelectors = [".brand", ".model", ".finish", ".ProductPriceDetails"];

// Create an array with the column selectors at the top to act as a header
[columnSelectors.join(",")].concat(
    // Grab the product details and arrayify it so we can use standard array functions
    $(".prod-details").toArray()
        // These are elements, and we want to be able to use jquery with them, so wrap each element with jquery
        .map(d => $(d))
        // For each prod-detail element, we want to extract each of the columns
        .map(d =>
            columnSelectors.map(
                // ... so we iterate the selectors and apply each one to the product, grab the text and trim() it to remove whitespace
                sel => d.find(sel).text().trim()
            ).join(",") // Finally join each of the columns with a comma so it follows CSV format
        )
).join("\n"); // And join all the rows with newlines

关于javascript - 如何从嵌套 div 标签中抓取数据并从开发工具导出到 CSV,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40934474/

相关文章:

javascript - 如何在 webpack 上获取原始静态 html 文件

javascript - 如何使用从服务器获取的客户端 JavaScript 来压缩图像?

javascript - 简单的 Ajax uploader 插件停止工作

c# - 在使用分页和 JavaScript 链接时,如何从 ASP.NET 网站上抓取信息?

javascript - Chrome 无法识别 Access-Control-Allow-Origin

Javascript Canvas 刷新闪烁

javascript - DIV 如何像 Javascript 中的另一个元素一样工作?

css - 为什么 chrome 会突出显示样式或计算中未列出的边距

python - 用 python 抓取 google resultstats

c# - 计算网站上HTML元素的面积?