簡而言之,我正在從網站上抓取資料并將其存盤在資料庫中。
相關欄位是鏈接、名稱、價格和專案條件。
我現在處理這個問題的方式是遍歷每個元素并將它們推送到各自的串列中。然后使用 For 回圈將其添加到資料庫中。因此,例如:
var names= [];
$(".midbox .framebox .frameboxcells .displaybox .displayboxbottom .dt.bg0 .serptablecell2-adv .serptablebasestyle2 .valtitle.lovewrap.padr4 .underlinedlinks").each(function(){
names.push($(this).text());
});
...
for (x in names){
var sql = "REPLACE INTO `item` (`link`, `title`, `price`, `date`, `item_condition`, `country`) VALUES (?)";
var values = [links[x], names[x], prices[x], '', states[x], cc];
con.query(sql, [values], function(err, result){
if (err) throw err;
});
}
這是非常幼稚的,因為它希望所有元素都存在并且它們完美對齊,到目前為止效果很好,直到我注意到我正在抓取的網站上的一些串列沒有專案條件元素,所以它被跳過并且串列不同步,導致配對錯誤的值。
我知道我正在尋找的答案與 .each 函式有關,但我不確定如何去做。我想我必須去最高點,.midbox .framebox .frameboxcells .displaybox .displayboxbottom .dt.bg0 .serptablecell2-adv .serptablebasestyle2從那里開始。如果找不到元素,則添加 NULL 值。
以下是完整(相關)代碼:
const $ = c.load(response.data);
$(".midbox .framebox .frameboxcells .displaybox .displayboxbottom .dt.bg0 .serptablecell2-adv .serptablebasestyle2 .splittable .splittablecell1 .padr2.bhserp-txt1.bhserp-new1").each(function(){
var fixedStr = $(this).text().replace(/,|£|\$|\s|[(GBP)]|[(USD)]/g, '');
prices.push(Number(fixedStr));
});
$(".midbox .framebox .frameboxcells .displaybox .displayboxbottom .dt.bg0 .serptablecell2-adv .serptablebasestyle2 .valtitle.lovewrap.padr4 .underlinedlinks").each(function(){
names.push($(this).text());
});
$(".midbox .framebox .frameboxcells .displaybox .displayboxbottom .dt.bg0 .serptablecell2-adv .serptablebasestyle2 .splittable .splittablecell1.bhserp-txt1 .padl1.labinfo").each(function(){
if ($(this)){
states.push($(this).text());
}
else{
console.log("Mistake here, pick me up!"); // I understand what I'm doing here does not make sense and is wrong as I've stated, but since that's what made me realize what I needed to do, I'm leaving it.
states.push("None");
}
});
$(".midbox .framebox .frameboxcells .displaybox .displayboxbottom .dt.bg0 .serptablecell2-adv .serptablebasestyle2 .valtitle.lovewrap.padr4 .underlinedlinks").each(function(){
var tempLink = $(this).attr('href');
var fixedLinks = tempLink.split("=");
var fixedLinks = fixedLinks[1].split("&");
links.push("https://www.ebay.co.uk/itm/" fixedLinks[0]);
});
...
con.connect(function(err){
if (err) throw err;
console.log("Connected!");
for (x in names){
var sql = "REPLACE INTO `item` (`link`, `title`, `price`, `date`, `item_condition`, `country`) VALUES (?)";
var values = [links[x], names[x], prices[x], '', states[x], cc];
con.query(sql, [values], function(err, result){
if (err) throw err;
});
}
});
uj5u.com熱心網友回復:
您應該迭代元素。如果您嘗試將價格與鏈接分開獲取,您將獲得糟糕的體驗。就像是:
for(let div of $('.product').get()){
let item = {
link: $(div).find('a').attr('href')
price: $(div).find('.price').text(),
}
// insert item into the db
}
uj5u.com熱心網友回復:
pguardiario 的回答完美,我將在這里留下我最終得到的代碼以供將來參考:
for(let div of $('.midbox .framebox .frameboxcells .displaybox .displayboxbottom .dt.bg0 .serptablecell2-adv .serptablebasestyle2').get()){
var tempLink = $(div).find('.underlinedlinks').attr('href');
var fixedLinks = tempLink.split("=");
var fixedLinks = fixedLinks[1].split("&");
var fixedStr = $(div).find('.padr2.bhserp-txt1.bhserp-new1').text().replace(/,|£|\$|\s|[(GBP)]|[(USD)]/g, '');
let item = {
link: "https://www.ebay.co.uk/itm/" fixedLinks[0],
name: $(div).find('.valtitle.lovewrap.padr4 .underlinedlinks').text(),
price: Number(fixedStr),
condition: $(div).find('.padl1.labinfo').text()
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/439220.html
標籤:javascript 节点.js 表示 axios 切里奥
