【发布时间】:2020-05-09 20:43:10
【问题描述】:
我正在构建一个网络爬虫。我有一个选择器的 json 对象,我需要对其进行迭代,以便抓取页面上的每个值并捕获数据。
我将如何创建一个 lodash 函数以递归方式遍历每个属性并执行标准函数以根据选择器获取元素内部文本值?
let startingJson = {
address:"tbody > tr > td:nth-of-type(4) > p",
comps: [{
address: "tbody > tr:nth-of-type(1) > td:nth-of-type(1) > p",
link: "tbody > tr:nth-of-type(1) > td:nth-of-type(1) > p > a",
dateLastSold: "tbody > tr:nth-of-type(1) > td:nth-of-type(2) > p",
value: "tbody > tr:nth-of-type(1) > td:nth-of-type(3) > p"
},
{
address: "tbody > tr:nth-of-type(2) > td:nth-of-type(1) > p",
link: "tbody > tr:nth-of-type(2) > td:nth-of-type(1) > p > a",
dateLastSold: "tbody > tr:nth-of-type(2) > td:nth-of-type(2) > p",
value: "tbody > tr:nth-of-type(2) > td:nth-of-type(3) > p"
}]
}
let finalJsonExample = {
address:"123 Main Street",
comps: [{
address: "234 Main Street",
link: "abc.com",
dateLastSold: "10/20/19",
value: "100000"
},
{
address: "345 Main Street",
link: "def.com",
dateLastSold: "10/21/19",
value: "110000"
}]
}
【问题讨论】:
-
请向我们展示您的尝试。 SO 不是免费的代码编写服务。此处的目标是帮助您在尝试无法正常工作时修复自己的尝试……而不是为您完成所有工作
标签: javascript arrays lodash