我无法抓取图像..我还抓取了其他数据

时间:2019-06-02 19:05:00

标签: node.js web-scraping

我无法从该网站上抓取图片。.我还抓取了其他数据..我正在用cheerio抓取。我需要我的项目的图片链接..任何帮助都将很好..谢谢

<div class="item-thumb has-frames"><a href="/bn/ad/apple-iphone-x-used-for-sale-dhaka-1063"><img alt="মোবাইল ফোন-ঢাকা - Apple iPhone X (Used)" title="Apple iPhone X (Used)" srcset="//i.bikroy-st.com/8b6a99f9-0127-4b29-8a42-d51a09ee220c/136/102/cropped.webp 1x, //i.bikroy-st.com/8b6a99f9-0127-4b29-8a42-d51a09ee220c/272/204/cropped.webp 1.3x"></a></div>


const fetch = require("node-fetch");
const cheerio = require("cheerio");

const url = "https://bikroy.com/bn/ads?query=";

const searchCache = {};

function searchProducts(searchTerm) {
    return fetch(`${url}${searchTerm}`)
        .then(response => response.text())
        .then(body => {
            const products = [];
            const $ = cheerio.load(body);
            $(".item-content").each(function(i, element) {
                const $element = $(element);
                const $title = $element.find(".item-title");
                const $location = $element.find(".item-location");
                const $price = $element.find(".item-info");
                const $link = $element.find("a");
                const $image = $element.find(".item-thumb has-frames");
                const $bikroy = "https://bikroy.com";

                const product = {
                    title: $title.text(),
                    location: $location.text(),
                    price: $price.text(),
                    link: $bikroy + $link.attr("href"),
                    image: $image.attr("srcset")
                };
                products.push(product);
            });

            return products;
        });
}

module.exports = {
    searchProducts
};

0 个答案:

没有答案