【问题标题】:Get image-urls from imgur link in node js从节点 js 中的 imgur 链接获取图像 URL
【发布时间】:2021-09-18 19:49:38
【问题描述】:

我需要从 imgur post urls 中获取图片 urls。

帖子的网址如下所示:https://imgur.com/gallery/kgfDi

但是图片的网址是这样的:https://i.imgur.com/DWVuwMc.jpeg

我尝试使用网络抓取库 x-ray js,但由于某种原因它不起作用。出于某种原因,我只得到一个空数组。

我的代码(我使用 Firebase Cloud Functions):

    var Xray = require('x-ray');

    exports.getImgurLink = functions.https.onCall(async (data, context) => {
        var xray = Xray();
    
        xray('https://imgur.com/a/1QEKZ2f', '.image-placeholder')(function (err, res) {
            console.log("res: "+res);
        })
    });

我对这篇帖子的 X 射线有想法:Make imgur link have .png or .jpg in the end of the url with nodeJS,但它已经老了,所以我不确定它是否仍然有效。

谁能告诉我另一种从 imgur 获取图像 url 的方法或如何修复我的 xray 代码?

感谢您的宝贵时间。

【问题讨论】:

    标签: javascript node.js x-ray


    【解决方案1】:

    您可以使用 puppeteer 来做到这一点。使用您的示例链接:

    const puppeteer = require('puppeteer')
    
    exports.getImgurLink = functions.https.onCall(async (data, context) => {
        let url = "https://imgur.com/gallery/kgfDi"
    
        const browser = await puppeteer.launch()
        const page = await browser.newPage()
        await page.goto(url)
    
        const el = await page.$x('//*[@id="root"]/div/div[1]/div[1]/div[3]/div/div[1]/div[2]/div/div/div[2]/div/div/div/div/div/img')
    
        el.forEach(async function(element){
            const src = await element.getProperty('src')
            const scrText = await src.jsonValue()
    
            console.log({ scrText })
        })
    });
    

    【讨论】:

      猜你喜欢
      • 2017-12-02
      • 2015-04-20
      • 2011-08-10
      • 2021-11-03
      • 1970-01-01
      • 2016-09-06
      • 2015-01-16
      • 2020-08-24
      • 1970-01-01
      相关资源
      最近更新 更多