知乎问答内容提取器油猴子脚本

IT 文章1个月前更新 小编
4 0 0

有时候需要扒取知乎的问答内容让AI来帮忙写一篇综合性较强的文章,于是让AI帮我写了个油猴子脚本,可以一键提取标题和所有回答内容,然后下载到本地的txt文件,记录下这个油猴子脚本:

// ==UserScript==
// @name         知乎问答内容导出工具
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  提取知乎问答的问题及所有答案内容,并导出为TXT文件
// @author       您的名字
// @match        https://www.zhihu.com/question/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // 创建导出按钮
    function createExportButton() {
        const button = document.createElement('button');
        button.textContent = '导出问答内容';
        button.style.position = 'fixed';
        button.style.top = '20px';
        button.style.right = '20px';
        button.style.zIndex = '9999';
        button.style.padding = '10px 15px';
        button.style.backgroundColor = '#0f88eb';
        button.style.color = 'white';
        button.style.border = 'none';
        button.style.borderRadius = '4px';
        button.style.cursor = 'pointer';
        button.style.fontSize = '14px';
        button.style.boxShadow = '0 2px 6px rgba(0,0,0,0.1)';

        // 添加按钮点击事件
        button.addEventListener('click', exportContent);

        // 将按钮添加到页面
        document.body.appendChild(button);
    }

    // 提取问题标题
    function getQuestionTitle() {
        const titleElement = document.querySelector('h1.QuestionHeader-title');
        return titleElement ? titleElement.textContent.trim() : '未找到问题标题';
    }

    // 提取所有答案
    function getAnswers() {
        const answerElements = document.querySelectorAll('.ContentItem.AnswerItem');
        const answers = [];

        answerElements.forEach((element, index) => {
            // 提取作者
            const authorMeta = element.querySelector('.AuthorInfo meta[itemprop="name"]');
            const author = authorMeta ? authorMeta.content : `匿名用户${index + 1}`;

            // 提取回答内容
            const contentElement = element.querySelector('.RichText.ztext.CopyrightRichText-richText');
            let content = '';

            if (contentElement) {
                // 处理段落
                const paragraphs = contentElement.querySelectorAll('p');
                paragraphs.forEach(p => {
                    content += p.textContent.trim() + '\n\n';
                });

                // 去除多余空行
                content = content.trim();
            } else {
                content = '未找到回答内容';
            }

            answers.push({
                author,
                content
            });
        });

        return answers;
    }

    // 导出内容为TXT文件
    function exportContent() {
        // 获取问题和答案
        const questionTitle = getQuestionTitle();
        const answers = getAnswers();

        // 构建文本内容
        let textContent = `问题:${questionTitle}\n\n`;
        textContent += `共${answers.length}个回答\n\n`;

        answers.forEach((answer, index) => {
            textContent += `--- 回答 ${index + 1} ---`;
            textContent += `\n作者:${answer.author}\n\n`;
            textContent += `内容:\n${answer.content}\n\n`;
            textContent += `--- 回答结束 ---\n\n`;
        });

        // 创建下载链接
        const blob = new Blob([textContent], { type: 'text/plain' });
        const url = URL.createObjectURL(blob);
        const a = document.createElement('a');
        a.href = url;

        // 生成文件名(使用问题标题的前20个字符)
        const fileName = `${questionTitle.substring(0, 20).replace(/[\/:*?"<>|]/g, '')}_知乎问答.txt`;
        a.download = fileName;

        // 触发下载
        document.body.appendChild(a);
        a.click();

        // 清理
        setTimeout(() => {
            document.body.removeChild(a);
            URL.revokeObjectURL(url);
        }, 0);
    }

    // 页面加载完成后添加按钮
    window.addEventListener('load', function() {
        // 等待页面完全加载
        setTimeout(createExportButton, 2000);
    });
})();

有需要的朋友也可以拿去使用!

优化了下,支持文章和问答:

ad

程序员导航

优网导航旗下整合全网优质开发资源,一站式IT编程学习与工具大全网站

// ==UserScript==
// @name         知乎问答内容导出工具
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  提取知乎问答的问题及所有答案内容,并导出为TXT文件
// @author       您的名字
// @match        https://*.zhihu.com/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // 创建导出按钮
    function createExportButton() {
        const button = document.createElement('button');
        button.textContent = '导出知乎内容';
        button.style.position = 'fixed';
        button.style.top = '20px';
        button.style.right = '20px';
        button.style.zIndex = '9999';
        button.style.padding = '10px 15px';
        button.style.backgroundColor = '#0f88eb';
        button.style.color = 'white';
        button.style.border = 'none';
        button.style.borderRadius = '4px';
        button.style.cursor = 'pointer';
        button.style.fontSize = '14px';
        button.style.boxShadow = '0 2px 6px rgba(0,0,0,0.1)';

        // 添加按钮点击事件
        button.addEventListener('click', exportContent);

        // 将按钮添加到页面
        document.body.appendChild(button);
    }

    // 提取问题标题
    function getQuestionTitle() {
        const titleElement = document.querySelector('h1.QuestionHeader-title');
        return titleElement ? titleElement.textContent.trim() : '未找到问题标题';
    }

    // 提取所有答案
    function getAnswers() {
        const answerElements = document.querySelectorAll('.ContentItem.AnswerItem');
        const answers = [];

        answerElements.forEach((element, index) => {
            // 提取作者
            const authorMeta = element.querySelector('.AuthorInfo meta[itemprop="name"]');
            const author = authorMeta ? authorMeta.content : `匿名用户${index + 1}`;

            // 提取回答内容
            const contentElement = element.querySelector('.RichText.ztext.CopyrightRichText-richText');
            let content = '';

            if (contentElement) {
                // 处理段落
                const paragraphs = contentElement.querySelectorAll('p');
                paragraphs.forEach(p => {
                    content += p.textContent.trim() + '\n\n';
                });

                // 去除多余空行
                content = content.trim();
            } else {
                content = '未找到回答内容';
            }

            answers.push({
                author,
                content
            });
        });

        return answers;
    }
     // 提取文章标题
    function getArticleTitle() {
        const titleElement = document.querySelector('h1.Post-Title');
        return titleElement ? titleElement.textContent.trim() : '未找到文章标题';
    }
    // 提取文章
    function getArticle() {
        const articleElement = document.querySelector('article.Post-Main');
        return articleElement.textContent.trim();
    }
    function isQuestionPage() {
        return /^\/question\/\d+$/.test(window.location.pathname);
    }

    function isArticlePage() {
        return /^\/p\/\d+$/.test(window.location.pathname);
    }
    // 导出内容为TXT文件
    function exportContent() {
        let textContent = "";
        let source = "";
        let title = "";
        if(isQuestionPage()){
            // 获取问题和答案
            title = getQuestionTitle();
            const answers = getAnswers();
            source = "知乎问答";
            // 构建文本内容
            textContent = `问题:${title}\n\n`;
            textContent += `共${answers.length}个回答\n\n`;

            answers.forEach((answer, index) => {
                textContent += `--- 回答 ${index + 1} ---`;
                textContent += `\n作者:${answer.author}\n\n`;
                textContent += `内容:\n${answer.content}\n\n`;
                textContent += `--- 回答结束 ---\n\n`;
            });
        }else if(isArticlePage()){
           source = "知乎文章";
           title = getArticleTitle()
           textContent = getArticle();
        }
        // 创建下载链接
        const blob = new Blob([textContent], { type: 'text/plain' });
        const url = URL.createObjectURL(blob);
        const a = document.createElement('a');
        a.href = url;

        // 生成文件名(使用问题标题的前20个字符)
        const fileName = `${title.substring(0, 20).replace(/[\/:*?"<>|]/g, '')}_${source}.txt`;
        a.download = fileName;

        // 触发下载
        document.body.appendChild(a);
        a.click();

        // 清理
        setTimeout(() => {
            document.body.removeChild(a);
            URL.revokeObjectURL(url);
        }, 0);
    }

    // 页面加载完成后添加按钮
    window.addEventListener('load', function() {
        // 等待页面完全加载
        setTimeout(createExportButton, 2000);
    });
})();

 

后续优化新增其他平台:

// ==UserScript==
// @name         知乎问答内容导出工具
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  提取知乎问答的问题及所有答案内容,并导出为TXT文件
// @author       您的名字
// @match        https://*.zhihu.com/*
// @match        https://www.jb51.net/blockchain/*
// @match        https://www.120btc.com/*
// @match        https://learnblockchain.cn/*
// @grant        none
// ==/UserScript==
(function() {
'use strict';
// 创建导出按钮
function createExportButton() {
const button = document.createElement('button');
button.textContent = '导出内容';
button.style.position = 'fixed';
button.style.top = '20px';
button.style.right = '20px';
button.style.zIndex = '9999999';
button.style.padding = '10px 15px';
button.style.backgroundColor = '#0f88eb';
button.style.color = 'white';
button.style.border = 'none';
button.style.borderRadius = '4px';
button.style.cursor = 'pointer';
button.style.fontSize = '14px';
button.style.boxShadow = '0 2px 6px rgba(0,0,0,0.1)';
// 添加按钮点击事件
button.addEventListener('click', exportContent);
// 将按钮添加到页面
document.body.appendChild(button);
}
// 提取问题标题
function getQuestionTitle() {
const titleElement = document.querySelector('h1.QuestionHeader-title');
return titleElement ? titleElement.textContent.trim() : '未找到问题标题';
}
// 提取所有答案
function getAnswers() {
const answerElements = document.querySelectorAll('.ContentItem.AnswerItem');
const answers = [];
answerElements.forEach((element, index) => {
// 提取作者
const authorMeta = element.querySelector('.AuthorInfo meta[itemprop="name"]');
const author = authorMeta ? authorMeta.content : `匿名用户${index + 1}`;
// 提取回答内容
const contentElement = element.querySelector('.RichText.ztext.CopyrightRichText-richText');
let content = '';
if (contentElement) {
// 处理段落
const paragraphs = contentElement.querySelectorAll('p');
paragraphs.forEach(p => {
content += p.textContent.trim() + '\n\n';
});
// 去除多余空行
content = content.trim();
} else {
content = '未找到回答内容';
}
answers.push({
author,
content
});
});
return answers;
}
// 提取文章标题
function getArticleTitle() {
const titleElement = document.querySelector('h1.Post-Title');
return titleElement ? titleElement.textContent.trim() : '未找到文章标题';
}
// 提取文章
function getArticle() {
const articleElement = document.querySelector('article.Post-Main');
return articleElement.textContent.trim();
}
function isQuestionPage() {
return /^\/question\/\d+$/.test(window.location.pathname);
}
function isArticlePage() {
return /^\/p\/\d+$/.test(window.location.pathname);
}
function isZhihu() {
return window.location.hostname.includes('zhihu.com');
}
function isJb51() {
return window.location.hostname.includes('jb51.net');
}
function is120btc() {
return window.location.hostname.includes('120btc.com');
}
function isLearnblockchain() {
return window.location.hostname.includes('learnblockchain.cn');
}
// 导出内容为TXT文件
function exportContent() {
if(isZhihu()){
exportZhihuContent();
}else if(isJb51()) {
exportJb51Content();
}else if(is120btc()) {
export120btcContent();
}else if(isLearnblockchain()){
exportLearnblockchainContent();
}
}
// 导出登链社区文章
function exportLearnblockchainContent() {
let source="登链社区";
const titleElement = document.getElementsByClassName('title')[0];
const title = titleElement ? titleElement.textContent.trim() : null;
// 获取正文内容(details)
const contentElement = document.getElementById('md_view');
const textContent = contentElement ? contentElement.innerText.trim() : null;
// 创建下载链接
const blob = new Blob([textContent], { type: 'text/plain' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
// 生成文件名(使用问题标题的前20个字符)
const fileName = `${title.replace(/[\/:*?"<>|]/g, '')}_${source}.txt`;
a.download = fileName;
// 触发下载
document.body.appendChild(a);
a.click();
// 清理
setTimeout(() => {
document.body.removeChild(a);
URL.revokeObjectURL(url);
}, 0);
}
// 导出120btc文章
function export120btcContent() {
let source="币圈子";
const titleElement = document.getElementsByClassName('title')[0];
const title = titleElement ? titleElement.textContent.trim() : null;
// 获取正文内容(details)
const contentElement = document.getElementsByClassName('details')[0];
const textContent = contentElement ? contentElement.innerText.trim() : null;
// 创建下载链接
const blob = new Blob([textContent], { type: 'text/plain' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
// 生成文件名(使用问题标题的前20个字符)
const fileName = `${title.replace(/[\/:*?"<>|]/g, '')}_${source}.txt`;
a.download = fileName;
// 触发下载
document.body.appendChild(a);
a.click();
// 清理
setTimeout(() => {
document.body.removeChild(a);
URL.revokeObjectURL(url);
}, 0);
}
// 导出脚本之家文章
function exportJb51Content() {
let source="脚本之家";
const titleElement = document.getElementById('title');
const title = titleElement ? titleElement.textContent.trim() : null;
// 获取正文内容(div#content)
const contentElement = document.getElementById('content');
const textContent = contentElement ? contentElement.innerText.trim() : null;
// 创建下载链接
const blob = new Blob([textContent], { type: 'text/plain' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
// 生成文件名(使用问题标题的前20个字符)
const fileName = `${title.replace(/[\/:*?"<>|]/g, '')}_${source}.txt`;
a.download = fileName;
// 触发下载
document.body.appendChild(a);
a.click();
// 清理
setTimeout(() => {
document.body.removeChild(a);
URL.revokeObjectURL(url);
}, 0);
}
// 导出知乎文章
function exportZhihuContent() {
let textContent = "";
let source = "";
let title = "";
if(isQuestionPage()){
// 获取问题和答案
title = getQuestionTitle();
const answers = getAnswers();
source = "知乎问答";
// 构建文本内容
textContent = `问题:${title}\n\n`;
textContent += `共${answers.length}个回答\n\n`;
answers.forEach((answer, index) => {
textContent += `--- 回答 ${index + 1} ---`;
textContent += `\n作者:${answer.author}\n\n`;
textContent += `内容:\n${answer.content}\n\n`;
textContent += `--- 回答结束 ---\n\n`;
});
}else if(isArticlePage()){
source = "知乎文章";
title = getArticleTitle()
textContent = getArticle();
}
// 创建下载链接
const blob = new Blob([textContent], { type: 'text/plain' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
// 生成文件名(使用问题标题的前20个字符)
const fileName = `${title.replace(/[\/:*?"<>|]/g, '')}_${source}.txt`;
a.download = fileName;
// 触发下载
document.body.appendChild(a);
a.click();
// 清理
setTimeout(() => {
document.body.removeChild(a);
URL.revokeObjectURL(url);
}, 0);
}
// 页面加载完成后添加按钮
window.addEventListener('load', function() {
// 等待页面完全加载
setTimeout(createExportButton, 2000);
});
})();

 

h2转h3,去除hr换行标签脚本:

ad

AI 工具导航

优网导航旗下AI工具导航,精选全球千款优质 AI 工具集

// ==UserScript==
// @name          WordPress H3 to H2 Replacer
// @namespace    http://tampermonkey.net/
// @version      2025-06-26
// @description  try to take over the world!
// @author       You
// @match        https://www.uonce.com/wp-admin/*
// @match        https://pc.panziye.com/wp-admin/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=uonce.com
// @grant        none
// ==/UserScript==
(function() {
'use strict';
// Your code here...
// 等待页面加载完成
// 检查是否在WordPress编辑页面
if (document.getElementById('insert-media-button')) {
// 创建替换标题按钮
const replaceButton = document.createElement('button');
replaceButton.type = 'button';
replaceButton.className = 'button button-secondary';
replaceButton.textContent = '替换H3为H2';
// 添加按钮到媒体按钮区域
const mediaButtons = document.getElementById('insert-media-button');
if (mediaButtons) {
mediaButtons.after(replaceButton);
// 为按钮添加点击事件
replaceButton.addEventListener('click', function() {
replaceH3WithH2();
});
}
}
// 替换H3标签为H2标签的函数
function replaceH3WithH2() {
// 获取编辑器内容
// 尝试直接处理textarea内容
const contentTextarea = document.getElementById('content');
if (contentTextarea) {
const originalContent = contentTextarea.value;
const newContent = originalContent.replace(/<h3([^>]*)>/gi, '<h2$1>').replace(/<\/h3>/gi, '</h2>').replace(/<hr>/gi,"");
if (newContent !== originalContent) {
contentTextarea.value = newContent;
//alert(`成功替换H3标签为H2标签!`);
} else {
alert('未找到H3标签!');
}
} else {
alert('无法获取编辑器内容!');
}
}
})();

 

© 版权声明

相关文章

暂无评论

暂无评论...