【问题标题】:How to make axios.get synchronous?如何使 axios.get 同步?
【发布时间】:2020-10-10 18:17:30
【问题描述】:

这是我的 1337x.to 刮刀的 nodejs 代码。我想要做的是首先 scrape 搜索结果很好,然后从这些搜索结果中,获取种子的 url 并 scrape 似乎是磁铁 url 链接作为一个不同的函数工作,但是当我尝试在 getTorrent 函数中 scrape 那些值时,它只是返回 undefined 并且我似乎无法存储这些值。谁能帮我这个?我只需要在返回之前将磁力链接存储在数组中。

这是代码:

const axios = require('axios')
const cheerio = require('cheerio')

const baseurl = 'https://1337x.to'


var magneturl = 'sdkllfklajds'

var arr = []
var nameList = []
var urlList = []
var seedList = []
var leechList = []
var dateList = []
var sizeList = []

const getMagnet = (torurl) => {
    axios.get(torurl).then(({data}) => {
        var $ = cheerio.load(data)
        $ = cheerio.load($('.dropdown-menu').html())
        magneturl = $('a').last().attr('href')
    })
    return magneturl
}

const getTorrent = (searchurl) => {
    axios.get(searchurl).then(({ data }) => {
        const $ = cheerio.load(data)

        $('.coll-1.name').each((index, element) => {

            var tag = $(element).children('a').last()
            var name = tag.text()
            var url = tag.attr('href')

            getMagnet(url).then((data) => {
                urlList.push(data)
            })

            if(name.length) {
                nameList.push(name)
                //urlList.push(url)
            }
        })

        $('.coll-2').each((index, element) => {
            var seeds = $(element).text()
            if(index > 0)
                seedList.push(seeds)
        })

        
        $('.coll-3').each((index, element) => {
            var leeches = $(element).text()
            if(index > 0)
                leechList.push(leeches)
        })

        
        $('.coll-date').each((index, element) => {
            var dateAdded = $(element).text()
            if(index > 0)
                dateList.push(dateAdded)
        })

        
        $('.coll-4.size').each((index, element) => {
            var size = $(element).text().replace($(element).children('span').text(), '')
            sizeList.push(size)
        })

        var i = 0

        while(i < 20) {
            arr.push({name: nameList[i], url: baseurl + urlList[i], seeds: seedList[i], leeches: leechList[i], dateAdded: dateList[i], size: sizeList[i]})
            i = i + 1
        }
    
    })
    return arr
}

module.exports = {getTorrent, getMagnet}

【问题讨论】:

    标签: javascript jquery node.js axios cheerio


    【解决方案1】:

    然后/等待你加倍。使用await 时,您可以这样做:

    const getMagnet = async (torurl) => {
        var data = await axios.get(torurl)
    
         var $ = cheerio.load(data)
          $ = cheerio.load($('.dropdown-menu').html())
          magneturl = $('a').last().attr('href')
         return magneturl
    }
    

    基本上直接使用await 的返回而不是then。在代码的其他地方你也有同样的东西。

    我认为如果你像这样清理你的代码,你可以在 Promise 中解开你的 Promise,这可能会解决你的问题。

    【讨论】:

    • 我的问题在于这段代码,但我也会尝试改变它。试过了,现在它正在返回一个未决的承诺。 async () => { await axios.get(url).then(({data}) => { var getdata = Cheerio.load(data) getdata = Cheerio.load(getdata('.dropdown-menu').html ()) urlList.push(getdata('a').last().attr('href')) }) }
    • 你真的应该阅读一下异步/等待。使用它们的目的是使包含 Promise 的代码更具可读性。即使在 Promise (then) 中,您也在使用 async/await,而最好只使用一种 Promise 编码。要么使用 async/await,要么使用 then,记住在 then 中你也可以返回新的 Promise。
    • 让我编辑代码并尝试。我将编辑上面的代码,所以一定要检查一下,因为我认为这不会解决我的问题。将 getMagnet 代码放在 getTorrent 代码中时,它不能正常工作。
    • 我非常非常接近放弃 nodejs。我被这个问题困扰了好几天,并没有找到真正的解决方案。 getMagnet 函数必须是独立的,或者它的代码必须写在 getTorrent 函数中。我都试过了,但我得到的只是承诺上的承诺。我让 getTorrent 以某种方式在没有 getMagnet 的情况下工作,但没有它就毫无用处。
    猜你喜欢
    • 2020-03-10
    • 2012-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-27
    • 2011-05-31
    • 2021-08-10
    相关资源
    最近更新 更多