|
发表于 2024-7-17 15:52:09
|
显示全部楼层
是的,有些网页内容是在页面初始加载时并没有完全渲染,或者一些数据是通过JavaScript动态加载的。这意味着在页面完全加载和动态内容渲染完成之前,直接查询DOM元素可能会找不到目标数据。这种情况在一些复杂的网站(例如 AJAX 或者 SPA 网站)上比较常见。
为了确保能够正确获取这些动态加载的数据,通常有以下几种解决方法:
### 方法一:使用 `MutationObserver`
`MutationObserver` 可以监视 DOM 的变化,当目标数据加载完成后再进行处理。
- // ==UserScript==
- // @name 导出网页信息到文件
- // @namespace http://tampermonkey.net/
- // @version 0.1
- // @description 导出当前网页中的部分信息到CSV文件
- // @author wp231957
- // @match *://*/*
- // @grant none
- // ==/UserScript==
- (function() {
- 'use strict';
-
- // 创建导出按钮
- const button = document.createElement('button');
- button.textContent = '导出表格数据';
- button.style.position = 'fixed';
- button.style.top = '10px';
- button.style.right = '10px';
- button.style.zIndex = 1000; // 确保按钮在其它元素之上
- document.body.appendChild(button);
- button.addEventListener('click', () => {
- const tableObserver = new MutationObserver(() => {
- const rows = Array.from(document.querySelectorAll('table tbody tr'));
- if (rows.length > 0) {
- alert('提取到的行数:' + rows.length);
-
- // 找到你需要的表格和列,比如提取第1和第3列(索引从0开始)
- const data = rows.map(row => {
- const cells = row.querySelectorAll('td');
- return [cells[0] ? cells[0].textContent : '',
- cells[2] ? cells[2].textContent : '']; // 提取第1和第3列
- });
- // 将数据转换为CSV格式
- const csvContent = "data:text/csv;charset=utf-8," + data.map(e => e.join(",")).join("\n");
- // 创建下载链接
- const encodedUri = encodeURI(csvContent);
- const link = document.createElement("a");
- link.setAttribute("href", encodedUri);
- link.setAttribute("download", "table_data.csv");
- document.body.appendChild(link);
- // 点击下载链接
- link.click();
- document.body.removeChild(link);
- // 停止观察
- tableObserver.disconnect();
- }
- });
- // 开始观察table元素的变化
- const targetNode = document.querySelector('table');
- if (targetNode) {
- tableObserver.observe(targetNode, { childList: true, subtree: true });
- } else {
- alert("无法找到目标表格");
- }
- });
- })();
复制代码
### 方法二:使用 `setTimeout` 或 `setInterval`
如果知道数据加载的大概时间,可以用 `setTimeout` 或 `setInterval` 延迟执行代码。
- // ==UserScript==
- // @name 导出网页信息到文件
- // @namespace http://tampermonkey.net/
- // @version 0.1
- // @description 导出当前网页中的部分信息到CSV文件
- // @author wp231957
- // @match *://*/*
- // @grant none
- // ==/UserScript==
- (function() {
- 'use strict';
- const waitForTableData = (callback) => {
- const interval = setInterval(() => {
- const rows = Array.from(document.querySelectorAll('table tbody tr'));
- if (rows.length > 0) {
- clearInterval(interval);
- callback(rows);
- }
- }, 1000); // 每秒检查一次
- }
- // 创建导出按钮
- const button = document.createElement('button');
- button.textContent = '导出表格数据';
- button.style.position = 'fixed';
- button.style.top = '10px';
- button.style.right = '10px';
- button.style.zIndex = 1000; // 确保按钮在其它元素之上
- document.body.appendChild(button);
- button.addEventListener('click', () => {
- waitForTableData((rows) => {
- alert('提取到的行数:' + rows.length);
-
- // 找到你需要的表格和列,比如提取第1和第3列(索引从0开始)
- const data = rows.map(row => {
- const cells = row.querySelectorAll('td');
- return [cells[0] ? cells[0].textContent : '',
- cells[2] ? cells[2].textContent : '']; // 提取第1和第3列
- });
- // 将数据转换为CSV格式
- const csvContent = "data:text/csv;charset=utf-8," + data.map(e => e.join(",")).join("\n");
- // 创建下载链接
- const encodedUri = encodeURI(csvContent);
- const link = document.createElement("a");
- link.setAttribute("href", encodedUri);
- link.setAttribute("download", "table_data.csv");
- document.body.appendChild(link);
- // 点击下载链接
- link.click();
- document.body.removeChild(link);
- });
- });
- })();
复制代码
这两种方法都能解决动态加载数据的问题,你可以选择适合自己网页情况的一种进行试验。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 |
|