簡體   English   中英

如何從當前獲取下一個重定向 url

[英]How can I get the next redirect url from current

當我訪問https://song.link/https://open.spotify.com/track/03f7xZmt2wHCIDJBFPK8G4?si=f4RrdDqMT52FqF0yJIfzFw 時,該鏈接有重定向...

我需要獲取下一個鏈接來進行實際的網頁抓取並獲取這些鏈接。

 var request = require('request') var cheerio = require('cheerio') request('https://song.link/https://open.spotify.com/track/03f7xZmt2wHCIDJBFPK8G4?si=f4RrdDqMT52FqF0yJIfzFw', function(err, resp, html) { if (!err){ const $ = cheerio.load(html) console.log(html) } })

你的時間

你需要 puppeteer 來做到這一點。

const puppeteer = require('puppeteer')

const getSecondURL = async firstURL => {

    const browser = await puppeteer.launch()
    const [page] = await browser.pages()

    await page.goto(firstURL, {waitUntil: 'networkidle0', timeout: 0})
    while(page.url === firstURL) {
        await page.waitFor(100)
    }
    console.log(page.url())

    await browser.close()
}

const url = process.argv.length > 2 ? process.argv[2] : 'https://song.link/https://open.spotify.com/track/03f7xZmt2wHCIDJBFPK8G4?si=f4RrdDqMT52FqF0yJIfzFw'
getSecondURL(url)

您可以通過將 url 放在命令行參數語法中來運行此腳本:

$ node script.js [urltoprocess]

其中 urltoprocess 是引號字符串中的 url 字符串。 例如:

$ node script.js 'https://song.link/https://open.spotify.com/track/03f7xZmt2wHCIDJBFPK8G4?si=f4RrdDqMT52FqF0yJIfzFw'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM