0

I need to get the image urls from imgur post urls.

The urls of the posts looks like this: https://i.stack.imgur.com/LPmGJ.jpg

But the image urls look like this: https://i.stack.imgur.com/HyVaJ.jpg

I tryed it with the web-scraping library x-ray js but for some reason it doesn't work. For some reason I just get an empty array.

My code (I use 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);
        })
    });

I have the idea with the xray from this Post: Make imgur link have .png or .jpg in the end of the url with nodeJS but it is already older so I am not sure if it still works.

Can someone show me another method to get image urls from imgur or how to fix my xray code?

Thank you for your time.

1 Answers1

1

You can use puppeteer to do that. With your example links:

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 })
    })
});
FriendlyMushroom
  • 178
  • 1
  • 10