gpt4 book ai didi

javascript - 使用 cheerio 解析 Node JS 上的代码

转载 作者:行者123 更新时间:2023-11-30 20:30:33 24 4
gpt4 key购买 nike

我正在尝试用 cheerio 解析代码,但我需要一些帮助)

现在我的解析看起来像这样:

enter image description here

这是我想要查看代码的方式)如果您能帮助我,我将不胜感激! :)

[ { version: '1.3',
href: '/download-app/com.girlsbeautysalon.android_summerhairsalon/4_com.girlsbeautysalon.android_summerhairsalon_2016-12-26.apk/',
data: 'Dec. 26, 2016'},
{ version: ' 1.3 ',
href: '/download-app/com.girlsbeautysalon.android_summerhairsalon/2_com.girlsbeautysalon.android_summerhairsalon_2016-12-25.apk/',
data: 'Dec. 25, 2016'},
{ version: ' 1.2 ',
href: '/download-app/com.girlsbeautysalon.android_summerhairsalon/3_com.girlsbeautysalon.android_summerhairsalon_2016-08-26.apk/',
data: 'Aug. 26, 2016'} ]

我正在尝试解析的 HTML 代码:

<div class="row" style="margin-left: 2%; margin-right: 2%;">
<table class="striped" style="margin-left: auto; margin-right: auto;">
<tbody>
<tr><td><a title="download Summer Girl Hair Spa &amp; Salon 1.3 apk " onclick="ga('send', 'event', 'button', 'download_ver', 'com.girlsbeautysalon.android_summerhairsalon_2016-12-26.apk');" href="/download-app/com.girlsbeautysalon.android_summerhairsalon/4_com.girlsbeautysalon.android_summerhairsalon_2016-12-26.apk/">1.3</a></td><td>Dec. 26, 2016</td></tr>
<tr><td><a title="download Summer Girl Hair Spa &amp; Salon 1.3 apk " onclick="ga('send', 'event', 'button', 'download_ver', 'com.girlsbeautysalon.android_summerhairsalon_2016-12-25.apk');" href="/download-app/com.girlsbeautysalon.android_summerhairsalon/2_com.girlsbeautysalon.android_summerhairsalon_2016-12-25.apk/"> 1.3 </a></td><td>Dec. 25, 2016</td></tr>
<tr><td><a title="download Summer Girl Hair Spa &amp; Salon 1.2 apk " onclick="ga('send', 'event', 'button', 'download_ver', 'com.girlsbeautysalon.android_summerhairsalon_2016-08-26.apk');" href="/download-app/com.girlsbeautysalon.android_summerhairsalon/3_com.girlsbeautysalon.android_summerhairsalon_2016-08-26.apk/"> 1.2 </a></td><td>Aug. 26, 2016</td></tr>
</tbody>
</table>
</div>

我在 cheerio (NodeJs) 上解析的代码:

   const row = $('.row');
const version = [];
const href = [];
const data = [];

row.find('tr').each(function (i, e) {
version.push({"version": $(this).find("td a[title^='download']").text()});
href.push({"href": $(this).find("td a").attr('href')});
data.push({"data": $(this).find("td").eq(1).text()});
});

最佳答案

您可以使用 map 方法并返回对象数组,其中每个对象都是来自当前 tr 的数据。

const data = $('.row table tr').map(function() {
const a = $(this).find('td:first-child a');
const td = $(this).find('td:last-child');

return {
version: a.text(),
href: a.attr('href'),
date: td.text()
}
}).get();

console.log(data)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="row" style="margin-left: 2%; margin-right: 2%;">
<table class="striped" style="margin-left: auto; margin-right: auto;">
<tbody>
<tr><td><a title="download Summer Girl Hair Spa &amp; Salon 1.3 apk " onclick="ga('send', 'event', 'button', 'download_ver', 'com.girlsbeautysalon.android_summerhairsalon_2016-12-26.apk');" href="/download-app/com.girlsbeautysalon.android_summerhairsalon/4_com.girlsbeautysalon.android_summerhairsalon_2016-12-26.apk/">1.3</a></td><td>Dec. 26, 2016</td></tr>
<tr><td><a title="download Summer Girl Hair Spa &amp; Salon 1.3 apk " onclick="ga('send', 'event', 'button', 'download_ver', 'com.girlsbeautysalon.android_summerhairsalon_2016-12-25.apk');" href="/download-app/com.girlsbeautysalon.android_summerhairsalon/2_com.girlsbeautysalon.android_summerhairsalon_2016-12-25.apk/"> 1.3 </a></td><td>Dec. 25, 2016</td></tr>
<tr><td><a title="download Summer Girl Hair Spa &amp; Salon 1.2 apk " onclick="ga('send', 'event', 'button', 'download_ver', 'com.girlsbeautysalon.android_summerhairsalon_2016-08-26.apk');" href="/download-app/com.girlsbeautysalon.android_summerhairsalon/3_com.girlsbeautysalon.android_summerhairsalon_2016-08-26.apk/"> 1.2 </a></td><td>Aug. 26, 2016</td></tr>
</tbody>
</table>
</div>

关于javascript - 使用 cheerio 解析 Node JS 上的代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50378065/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com