1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
| let allResults = [];
async function scrapeAllPages() { try { await scrapeCurrentPage(); while (await goToNextPage()) { await scrapeCurrentPage(); } await showFinalResults(); } catch (error) { console.error('出错:', error); alert(`爬取中断: ${error.message}`); } }
function scrapeCurrentPage() { return new Promise(resolve => { setTimeout(() => { const elements = document.querySelectorAll('div.info__top a.title'); const pageResults = Array.from(elements).map(el => `biliv "${el.href}"`); allResults = [...allResults, ...pageResults]; console.log(`[进度] 当前页: ${pageResults.length}条 | 总计: ${allResults.length}条`); resolve(); }, 1000); }); }
function goToNextPage() { return new Promise(resolve => { setTimeout(() => { const buttons = [...document.querySelectorAll('button.vui_button')]; const nextBtn = buttons.find(btn => /下一页|下一頁|>|»/.test(btn.textContent.trim()) && !btn.disabled ); if (nextBtn) { console.log('[动作] 正在跳转下一页...'); nextBtn.click(); setTimeout(resolve, 2500, true); } else { console.log('[状态] 已是最后一页'); resolve(false); } }, 500); }); }
async function showFinalResults() { const resultText = allResults.join('\n'); console.log(`[完成] 共爬取 ${allResults.length} 条数据:\n${resultText}`); const div = document.createElement('div'); div.style = ` position: fixed; top: 20px; right: 20px; z-index: 99999; padding: 15px; background: #fff; box-shadow: 0 0 10px rgba(0,0,0,0.3); border-radius: 5px; font-family: sans-serif; max-width: 300px; `; div.innerHTML = ` <h3 style="margin-top:0">爬取完成</h3> <p>共 ${allResults.length} 条数据</p> <textarea id="result-data" style="width:100%; height:100px; margin:10px 0">${resultText}</textarea> <button id="copy-btn" style="padding:8px 15px; background:#07c; color:#fff; border:none; border-radius:4px"> 点击复制 </button> <button id="close-btn" style="margin-left:10px; padding:8px 15px">关闭</button> `; document.body.appendChild(div); div.querySelector('#copy-btn').addEventListener('click', async () => { try { await navigator.clipboard.writeText(resultText); alert('已成功复制到剪贴板!'); } catch (err) { const textarea = div.querySelector('#result-data'); textarea.select(); document.execCommand('copy'); alert('已使用备用方法复制!'); } }); div.querySelector('#close-btn').addEventListener('click', () => { div.remove(); }); }
scrapeAllPages();
|