无循环迭代函数

Iterating Function without Loop

提问人:dvtpetrosyan 提问时间:7/5/2023 最后编辑:dvtpetrosyan 更新时间:7/6/2023 访问量:68

问:

在这里,它必须获取初始网页中键入的标签内的每个 url。初始网页的html大致如下:<li>disc

<body>
  <li type="disc">
    <a href="url1">Lorem.</a>
    <a href="url2">Dolor</a>
    <a href="LAST_URL3">In!</a>
  </li>
  <li type="disc">
    <a href="url">Accusantium.</a>
    <a href="url">Dolor</a>
    <a href="url">Eveniet!</a>
  </li>
  <li type="disc">
    <a href="url">Asperiores?</a>
    <a href="url">Dolor</a>
    <a href="url">Suscipit!</a>
  </li>
  <li type="disc">
    <a href="url">Excepturi?</a>
    <a href="url">Dolor</a>
    <a href="url">Temporibus!</a>
  </li>
</body>

Javascript的:

    const w = await mani.metax.get(work).then(r => r.json())
    const url = w.source
    const response = await fetch( + url)
    const html = await response.text()

    async function parse() {
        const parser = new DOMParser()
        const doc = parser.parseFromString(html, 'text/html')

        booksNames = w.booksNames.split(', ')
        const booksList = url.includes('bible') ? doc.querySelectorAll('div.arm') : doc.querySelectorAll('li[type="disc"]')
        for (const book of booksList) {
            let bookObj = {"name": booksNames[0], "type": "7d998386-e38f-4d73-97f3-db9f1d7641d8-bf1523bf-09f1-4822-b546-9a10a8263ceb"}
            let bookObjStr = JSON.stringify(bookObj); booksNames.shift()
            let uuid = await mani.metax.save(bookObjStr)
            const b = await mani.metax.get(uuid).then(r => r.json()); b.uuid = uuid; b.chapters = []

            const chaptersList = Array.from(book.querySelectorAll('a'));

            let hrefsList = chaptersList.map(({href}) => ({href: href.replace('https://realschool.am:542/db/', url), done:false, doc:""}));
            let current
            const getData = async () => {
                if (hrefsList.length === 0) {
                    let versesList = Array.from(current.doc.querySelectorAll('table:nth-of-type(3) td:nth-of-type(1)'))
                    let chapObj = {"type": "4e680ac1-c1ac-408b-af50-103843ad1054-7b348fbb-0890-418d-b176-e090b76a8c50"}
                    let chapObjStr = JSON.stringify(chapObj)
                    let chapUUID = await mani.metax.save(chapObjStr)
                    const c = await mani.metax.get(chapUUID).then(r => r.json()); c.uuid = chapUUID; c.verses = []
                    c.name = versesList[0].innerText.trim().replace('``', '').slice(4); versesList.shift()
                    for (let verse of versesList) {
                        let text = verse.innerText.trim().replace('``', '')
                        let obj = {"name": text.split(' ')[0], "type": "06408198-7666-43f4-b2eb-6de5715434f9-f801e312-0957-4429-871b-7d448364c65c"}
                        let objStr = JSON.stringify(obj)
                        let verseUUID = await mani.metax.save(objStr)
                        const v = await mani.metax.get(verseUUID).then(r => r.json()); v.uuid = verseUUID; v.text = text
                        await mani.metax.update(verseUUID, JSON.stringify(v))
                        c.verses.push(verseUUID)
                    }
                    await mani.metax.update(chapUUID, JSON.stringify(c))
                    b.chapters.push(chapUUID) // Adding new object in a collection
                } else {
                    current = hrefsList[0];
                    const res = fetch(current.href).then(response => response.text()).then(text => {
                        const parser = new DOMParser()
                        current.doc = parser.parseFromString(html, 'text/html')
                    }).then(() => {
                        current.done = true;
                        console.log(current.href,'parsed successfully');
                        hrefsList = hrefsList.filter(({done}) => !done);
                        setTimeout(getData, 2000); // try next (or again)
                    })
                }
            }
            getData();

                let versesList = Array.from(doc.querySelectorAll('table:nth-of-type(3) td:nth-of-type(1)'))
                let chapObj = {"type": "4e680ac1-c1ac-408b-af50-103843ad1054-7b348fbb-0890-418d-b176-e090b76a8c50"}
                let chapObjStr = JSON.stringify(chapObj)
                let chapUUID = await mani.metax.save(chapObjStr)
                const c = await mani.metax.get(chapUUID).then(r => r.json()); c.uuid = chapUUID; c.verses = []
                c.name = versesList[0].innerText.trim().replace('``', '').slice(4); versesList.shift()
            //}
            await mani.metax.update(uuid, JSON.stringify(b))
            w.books.push(uuid) // Adding new object in a collection

            await mani.metax.update(work, JSON.stringify(w)).then(r => alert("New book complete"))
        }
    }

但是它实际上所做的是仅从第一个获取每个 url,然后在获取它导致 .因此,它似乎试图从中获取更多 URL,并且由于没有更多,它只是获取 .它必须迭代并开始从 second 开始获取 url,但事实并非如此。更重要的是,如果我们在调用函数后将任何测试消息输出到控制台,它将被输出,并将在成功获取每个 url 后输出的消息之前输出。如果我们在输出的 test 之前调用函数,则 test 消息不能在之后输出吗?如果它没有停留在函数上并继续到其余循环内容,那么为什么循环不迭代呢?<li>LAST_URL3Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'innerText')<li>undefined<li>getData()(some url) parsed successfullyparsed successfully

JavaScript 循环 获取 迭代 for-of-loop

评论

0赞 Bergi 7/5/2023
请注意,不会等待 .尝试编写一个实际的循环,而不是递归await getData();setTimeout(getData, 2000);
0赞 dvtpetrosyan 7/6/2023
所以这样:但它每次只解析一些 <li>s 的链接,比如 2 <li>s 或 3,或者所有 <li>s......const chaptersList = Array.from(book.querySelectorAll('a')); for (chapter of chaptersList) { const href = chapter.getAttribute('href').replace('../book/', '') let res = [] let ref = url + href const response = await fetch(corsProxy + ref) console.log(response) const html = await response.text() const parser = new DOMParser() const doc = parser.parseFromString(html, 'text/html') } console.log('parsed')

答: 暂无答案