我在解析和获取所需值时遇到问题
I am having trouble parsing and getting value wanted
我想做的是通过 html 解析并获取此 offerId。它在 href link 内,所以我打算做的只是将它转换成一个字符串,我稍后会做剩下的。首先,我需要做的是解析并获取该 href。我正在尝试从该网站 https://www.walmart.com/ip/Pokemon-Assorted-Lot-of-50-Single-Cards-Any-Series/127446742 获取它。如果你按 ctrl-f 并输入 offerId,你会发现我正在寻找的 href for.There 是其中的几个,但如果我能够弄清楚如何让其中一个得到另一个,那么应该不会太难.如果您需要任何额外说明,请 lmk。
function getOfferId (err, data){
const $ = cheerio.load(item.urltest);
const offerId1 = $('.seller-name').attr('href');
console.log(offerId1)
}
getOfferId()
我已经使用 Axios 完成了这项工作,实际上可以使用任何 http 客户端(node-fetch 等),我决定简单地遍历页面中的链接并对“offerid=”进行正则表达式测试,我们然后可以解析 hrefs 并提取 offerids。
const cheerio = require('cheerio');
const axios = require("axios");
async function getOfferIds(url) {
const { data: html } = await axios.get(url, { headers: { "User-Agent": "Axios"}});
const $ = cheerio.load(html);
const linkList = $('a').map( (index, element) => {
return { href: $(element).attr('href'), text: $(element).text() };
}).toArray().filter(({ href }) => /offerid=/i.test(href));
console.log("getOfferIds: linkList:", linkList);
const offerIds = linkList.map( ({ href, text }) => {
return { text, href, offerId: new URL(href, "a://").searchParams.get("offerId") };
});
console.log("getOfferIds: offerIds:", offerIds);
return offerIds;
}
async function test() {
const offerIds = await getOfferIds("https://www.walmart.com/ip/Pokemon-Assorted-Lot-of-50-Single-Cards-Any-Series/127446742");
console.log("test: offerIds:", offerIds);
}
test();
当您将页面作为字符串时,您可以使用正则表达式在其中搜索所需的文本。
这是一个例子:
const regexp = /offerid=(.+?)\W/gi;
const result = [...stringPage.matchAll(regexp)];
console.log('first found id:', result[0][1])
//first found id: 8911563635265
注意:变量前的“...”是将返回的对象转换为数组所必需的;使循环更容易。
我希望这可以帮助您入门。
我想做的是通过 html 解析并获取此 offerId。它在 href link 内,所以我打算做的只是将它转换成一个字符串,我稍后会做剩下的。首先,我需要做的是解析并获取该 href。我正在尝试从该网站 https://www.walmart.com/ip/Pokemon-Assorted-Lot-of-50-Single-Cards-Any-Series/127446742 获取它。如果你按 ctrl-f 并输入 offerId,你会发现我正在寻找的 href for.There 是其中的几个,但如果我能够弄清楚如何让其中一个得到另一个,那么应该不会太难.如果您需要任何额外说明,请 lmk。
function getOfferId (err, data){
const $ = cheerio.load(item.urltest);
const offerId1 = $('.seller-name').attr('href');
console.log(offerId1)
}
getOfferId()
我已经使用 Axios 完成了这项工作,实际上可以使用任何 http 客户端(node-fetch 等),我决定简单地遍历页面中的链接并对“offerid=”进行正则表达式测试,我们然后可以解析 hrefs 并提取 offerids。
const cheerio = require('cheerio');
const axios = require("axios");
async function getOfferIds(url) {
const { data: html } = await axios.get(url, { headers: { "User-Agent": "Axios"}});
const $ = cheerio.load(html);
const linkList = $('a').map( (index, element) => {
return { href: $(element).attr('href'), text: $(element).text() };
}).toArray().filter(({ href }) => /offerid=/i.test(href));
console.log("getOfferIds: linkList:", linkList);
const offerIds = linkList.map( ({ href, text }) => {
return { text, href, offerId: new URL(href, "a://").searchParams.get("offerId") };
});
console.log("getOfferIds: offerIds:", offerIds);
return offerIds;
}
async function test() {
const offerIds = await getOfferIds("https://www.walmart.com/ip/Pokemon-Assorted-Lot-of-50-Single-Cards-Any-Series/127446742");
console.log("test: offerIds:", offerIds);
}
test();
当您将页面作为字符串时,您可以使用正则表达式在其中搜索所需的文本。 这是一个例子:
const regexp = /offerid=(.+?)\W/gi;
const result = [...stringPage.matchAll(regexp)];
console.log('first found id:', result[0][1])
//first found id: 8911563635265
注意:变量前的“...”是将返回的对象转换为数组所必需的;使循环更容易。 我希望这可以帮助您入门。