【问题标题】:Web-scraping images with JavaScript Cheerio使用 JavaScript Cheerio 抓取图像
【发布时间】:2020-07-15 13:41:07
【问题描述】:

我在尝试从亚马逊网络抓取图像时收到错误消息。目标是下载书籍封面。

(node:26904) UnhandledPromiseRejectionWarning: Error: undefined is not a valid uri or options object.
    at request (C:\Users\Isaac\Desktop\webscrape_with_cheerio\node_modules\request\index.js:44:11)
    at PromisseHandle._RejectOrResolve (C:\Users\Isaac\Desktop\webscrape_with_cheerio\node_modules\node-image-downloader\src\image-downloader.js:86:5)
    at new Promise (<anonymous>)
    at ImageDownloader (C:\Users\Isaac\Desktop\webscrape_with_cheerio\node_modules\node-image-downloader\src\image-downloader.js:98:26)
    at Request._callback (C:\Users\Isaac\Desktop\webscrape_with_cheerio\index.js:22:13)
    at Request.self.callback (C:\Users\Isaac\Desktop\webscrape_with_cheerio\node_modules\request\request.js:185:22)
    at Request.emit (events.js:210:5)
    at Request.<anonymous> (C:\Users\Isaac\Desktop\webscrape_with_cheerio\node_modules\request\request.js:1154:10)
    at Request.emit (events.js:210:5)
    at IncomingMessage.<anonymous> (C:\Users\Isaac\Desktop\webscrape_with_cheerio\node_modules\request\request.js:1076:12)
(node:26904) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
(node:26904) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with 
a non-zero exit code.

到目前为止,这是我的代码。我相信我的错误源于这条线;

var imagesrc = $(".a-row center-align litb-on-click img").attr('src')

我需要访问图书网址所在的班级,尽管我可能错了。

const express = require('express');
const cheerio = require('cheerio');
const download = require('node-image-downloader');
const request = require('request');

const app = express();

app.get('/', (req, res) => {
    var url = "https://www.amazon.com/Black-Swan-Improbable-Robustness-Fragility/dp/081297381X"

    // makeing a request
    request(url,(error, response,html) => {
        if (!error){
            //console.log(html);
            var $ = cheerio.load(html)

            var imagesrc = $(".a-row center-align litb-on-click img").attr('src')

            //download the image

            download({
                imgs: [
                    {
                     uri:imagesrc                       
                    }
                ],
                dest:'./downloads'
            })
                .then((info) => {
                    console.log("Download Complete")
                    process.exit(1)
            })
        }
    })
})
app.listen(5000)

【问题讨论】:

  • 您能否在您的问题中添加足够的 HTML 来验证选择器? center-align 和 litb-on-click 应该以 . 为前缀作为类选择器

标签: javascript node.js web-scraping cheerio


【解决方案1】:

这里,a-row center-align litb-on-click 是类

要选择一个类,我们需要使用 class-selector 。 (点)

请尝试更新您的 imagesrc 变量,如下所示

var imagesrc = $(".a-row.center-align.litb-on-click img").attr('src');

空格不是强制性的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-01-29
    • 2021-11-27
    • 1970-01-01
    • 2020-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多