本文介绍了如何使用油猴(Tampermonkey)脚本进行网页数据抓取和自定义UI交互。油猴脚本是一种用户脚本管理器,允许用户在浏览器中运行自定义JavaScript代码,以增强网页功能或自动化任务。通过具体代码示例,学习者可以深入理解油猴脚本的工作原理,并掌握如何编写用于数据抓取的JavaScript爬虫。内容涵盖HTML、CSS和JavaScript的编写技巧,以及如何通过油猴脚本实现多页面数据抓取。本文旨在帮助学习者彻底掌握油猴脚本的使用方法,但强调仅用于学习目的,不得用于不当行为。
油猴脚本, 数据抓取, 自定义UI, JavaScript, 多页面
在互联网时代,网页的交互性和功能性变得越来越重要。为了满足用户的个性化需求,用户脚本管理器应运而生。用户脚本管理器是一种浏览器扩展工具,允许用户在浏览网页时运行自定义的JavaScript代码,从而增强网页的功能或实现特定的任务自动化。其中,油猴(Tampermonkey)是最受欢迎的用户脚本管理器之一。
油猴脚本管理器不仅支持多种浏览器,如Chrome、Firefox、Safari等,还提供了丰富的API和文档,使得开发者能够轻松地编写和管理用户脚本。通过油猴脚本,用户可以实现诸如自动填写表单、提取网页数据、修改网页样式等多种功能。这些功能不仅提高了用户的浏览体验,也为开发者提供了一个强大的工具,用于测试和调试网页应用。
安装油猴脚本管理器非常简单。首先,用户需要访问油猴的官方网站或浏览器的扩展商店,下载并安装油猴扩展。以Chrome浏览器为例,用户可以在Chrome Web Store中搜索“Tampermonkey”,点击“添加至Chrome”按钮即可完成安装。
安装完成后,用户可以通过浏览器的扩展管理页面打开油猴脚本管理器的设置界面。在这里,用户可以创建新的脚本、导入现有的脚本、管理已有的脚本等。创建新脚本时,用户需要填写一些基本信息,如脚本名称、描述、匹配的网址等。这些信息将帮助油猴脚本管理器确定何时运行该脚本。
接下来,用户可以在脚本编辑器中编写JavaScript代码。油猴脚本管理器提供了丰富的API,如GM_xmlhttpRequest
、GM_setValue
、GM_getValue
等,这些API可以帮助用户更方便地实现数据抓取和存储等功能。例如,使用GM_xmlhttpRequest
可以发起跨域请求,获取其他网站的数据;使用GM_setValue
和GM_getValue
可以存储和读取用户数据,实现持久化存储。
此外,油猴脚本管理器还支持版本控制和更新机制。用户可以将脚本发布到社区,分享给其他用户。当有新的版本发布时,油猴脚本管理器会自动提示用户更新,确保用户始终使用最新的脚本。
总之,油猴脚本管理器为用户和开发者提供了一个强大且灵活的工具,使得网页的个性化定制和任务自动化变得更加容易。通过学习和使用油猴脚本,用户不仅可以提升自己的编程技能,还能享受到更加便捷和个性化的上网体验。
在深入探讨如何使用油猴脚本进行网页数据抓取和自定义UI交互之前,我们先来回顾一下JavaScript的核心语法。JavaScript是一种广泛应用于网页开发的编程语言,它具有强大的功能和灵活性,能够实现从简单的网页交互到复杂的Web应用程序的各种功能。
JavaScript支持多种变量声明方式,包括var
、let
和const
。var
是最早的变量声明方式,但在现代JavaScript中,推荐使用let
和const
,因为它们具有块级作用域,可以避免变量污染问题。
// 使用var声明变量
var name = "张晓";
// 使用let声明变量
let age = 28;
// 使用const声明常量
const city = "上海";
JavaScript支持多种数据类型,包括字符串、数字、布尔值、数组、对象、null和undefined。
let str = "Hello, World!";
let num = 42;
let bool = true;
let arr = [1, 2, 3];
let obj = { name: "张晓", age: 28 };
let n = null;
let u = undefined;
JavaScript提供了多种控制结构,包括条件语句和循环语句。条件语句用于根据不同的条件执行不同的代码块,常见的条件语句有if
、else if
和else
。
if (age > 18) {
console.log("成年了");
} else {
console.log("未成年");
}
循环语句用于重复执行某段代码,常见的循环语句有for
、while
和do...while
。
// for循环
for (let i = 0; i < 5; i++) {
console.log(i);
}
// while循环
let count = 0;
while (count < 5) {
console.log(count);
count++;
}
函数是JavaScript中的重要组成部分,用于封装可重用的代码块。函数可以有参数和返回值,也可以没有。
function greet(name) {
return `Hello, ${name}!`;
}
console.log(greet("张晓")); // 输出: Hello, 张晓!
网页数据抓取,也称为网络爬虫,是指通过程序自动从网页中提取所需数据的过程。在现代社会,数据的重要性不言而喻,网页数据抓取技术成为了获取大量数据的重要手段。油猴脚本作为一种用户脚本管理器,可以利用JavaScript的强大功能,实现高效的网页数据抓取。
网页数据抓取的基本流程通常包括以下几个步骤:
在油猴脚本中,我们可以使用GM_xmlhttpRequest
API来发送HTTP请求,获取网页内容。
GM_xmlhttpRequest({
method: "GET",
url: "https://example.com",
onload: function(response) {
console.log(response.responseText);
}
});
除了油猴脚本,还有一些常用的网页数据抓取工具,如Python的requests
库和BeautifulSoup
库。这些工具提供了更高级的功能和更好的性能,适用于大规模的数据抓取任务。然而,对于个人用户来说,油猴脚本因其简单易用的特点,仍然是一个不错的选择。
HTML(HyperText Markup Language)是网页的结构语言,DOM(Document Object Model)则是浏览器中表示和操作HTML文档的一种方式。通过JavaScript,我们可以对HTML文档进行动态操作,实现各种交互效果。
在JavaScript中,选择HTML元素是DOM操作的基础。常见的选择元素的方法有getElementById
、getElementsByClassName
、getElementsByTagName
和querySelector
等。
// 通过ID选择元素
let elementById = document.getElementById("myId");
// 通过类名选择元素
let elementsByClass = document.getElementsByClassName("myClass");
// 通过标签名选择元素
let elementsByTag = document.getElementsByTagName("div");
// 通过CSS选择器选择元素
let elementBySelector = document.querySelector(".myClass");
选择到元素后,我们可以通过修改其属性、内容或样式来实现各种效果。常见的修改方法有innerHTML
、textContent
、setAttribute
和style
等。
// 修改元素的HTML内容
elementById.innerHTML = "<p>新的内容</p>";
// 修改元素的文本内容
elementById.textContent = "新的文本";
// 修改元素的属性
elementById.setAttribute("class", "newClass");
// 修改元素的样式
elementById.style.color = "red";
除了修改现有元素,我们还可以通过JavaScript创建新的元素或删除现有的元素。常见的方法有createElement
、appendChild
和removeChild
等。
// 创建新的元素
let newElement = document.createElement("div");
newElement.textContent = "这是一个新的元素";
// 将新元素添加到文档中
document.body.appendChild(newElement);
// 删除元素
document.body.removeChild(newElement);
通过以上介绍,我们可以看到,JavaScript和DOM操作是实现网页数据抓取和自定义UI交互的重要工具。掌握了这些基本技巧,我们就可以利用油猴脚本,轻松实现各种复杂的功能。希望本文能帮助读者更好地理解和应用这些技术,提升自己的编程能力。
在使用油猴脚本进行网页数据抓取和自定义UI交互的过程中,CSS样式定制是一个不可或缺的环节。通过精心设计的CSS样式,可以显著提升用户体验,使网页更加美观和易用。CSS(Cascading Style Sheets)是一种用于描述HTML文档样式的样式表语言,它允许开发者对网页的布局、颜色、字体等进行精细控制。
在油猴脚本中,可以通过GM_addStyle
API将自定义的CSS样式添加到当前页面。这使得开发者可以轻松地改变页面的外观,而无需直接修改HTML源代码。例如,如果想改变某个元素的背景色和字体颜色,可以使用以下代码:
GM_addStyle(`
#myElement {
background-color: #f0f0f0;
color: #333;
}
`);
除了静态的样式定制,油猴脚本还支持动态调整CSS样式。通过JavaScript,可以根据用户的操作或页面的状态实时改变样式。例如,当用户鼠标悬停在一个按钮上时,可以改变按钮的颜色:
document.getElementById('myButton').addEventListener('mouseover', function() {
this.style.backgroundColor = 'blue';
this.style.color = 'white';
});
document.getElementById('myButton').addEventListener('mouseout', function() {
this.style.backgroundColor = '';
this.style.color = '';
});
随着移动设备的普及,响应式设计成为网页开发的重要趋势。通过媒体查询(Media Queries),可以针对不同设备和屏幕尺寸应用不同的样式。在油猴脚本中,可以使用以下代码实现响应式设计:
GM_addStyle(`
@media (max-width: 600px) {
#myElement {
font-size: 14px;
}
}
@media (min-width: 601px) {
#myElement {
font-size: 18px;
}
}
`);
自定义UI组件是提升网页交互性和用户体验的关键。通过油猴脚本,开发者可以创建各种自定义组件,如弹出窗口、下拉菜单、滑动条等,使网页更加丰富和互动。
弹出窗口是一种常见的UI组件,用于显示额外的信息或提示。在油猴脚本中,可以使用HTML和JavaScript创建一个简单的弹出窗口:
<div id="popup" style="display: none; position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%); background: white; padding: 20px; border: 1px solid #ccc;">
<p>这是弹出窗口的内容</p>
<button id="closePopup">关闭</button>
</div>
document.getElementById('showPopup').addEventListener('click', function() {
document.getElementById('popup').style.display = 'block';
});
document.getElementById('closePopup').addEventListener('click', function() {
document.getElementById('popup').style.display = 'none';
});
下拉菜单是另一种常用的UI组件,用于提供更多的选项或功能。在油猴脚本中,可以使用HTML和JavaScript创建一个简单的下拉菜单:
<div class="dropdown">
<button id="dropdownButton">菜单</button>
<div id="dropdownContent" style="display: none; position: absolute; background: white; border: 1px solid #ccc;">
<a href="#">选项1</a>
<a href="#">选项2</a>
<a href="#">选项3</a>
</div>
</div>
document.getElementById('dropdownButton').addEventListener('click', function() {
document.getElementById('dropdownContent').style.display = 'block';
});
document.addEventListener('click', function(event) {
if (!event.target.matches('.dropdown *')) {
document.getElementById('dropdownContent').style.display = 'none';
}
});
优秀的用户交互体验是网页成功的关键。通过油猴脚本,开发者可以实现各种交互效果,提升用户的满意度和使用效率。
实时反馈是提高用户满意度的有效手段。当用户进行某些操作时,及时给予反馈可以增强用户的信心和满意度。例如,当用户提交表单时,可以显示一个加载动画,告知用户正在处理请求:
<form id="myForm">
<input type="text" name="username" placeholder="用户名">
<button type="submit">提交</button>
<div id="loading" style="display: none;">正在加载...</div>
</form>
document.getElementById('myForm').addEventListener('submit', function(event) {
event.preventDefault();
document.getElementById('loading').style.display = 'block';
GM_xmlhttpRequest({
method: "POST",
url: "https://example.com/submit",
data: new URLSearchParams(new FormData(this)).toString(),
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
onload: function(response) {
document.getElementById('loading').style.display = 'none';
alert('提交成功');
}
});
});
错误处理是确保用户体验的重要环节。当用户输入无效数据或发生其他错误时,应及时给出明确的提示,帮助用户纠正错误。例如,当用户输入的用户名不符合要求时,可以显示一个错误消息:
<form id="myForm">
<input type="text" name="username" placeholder="用户名">
<div id="error" style="color: red; display: none;">用户名必须包含字母和数字</div>
<button type="submit">提交</button>
</form>
document.getElementById('myForm').addEventListener('submit', function(event) {
event.preventDefault();
let username = document.querySelector('input[name="username"]').value;
if (!/^[a-zA-Z0-9]+$/.test(username)) {
document.getElementById('error').style.display = 'block';
} else {
document.getElementById('error').style.display = 'none';
// 提交表单
}
});
通过以上介绍,我们可以看到,CSS样式定制、自定义UI组件设计和用户交互体验优化是使用油猴脚本进行网页数据抓取和自定义UI交互的重要方面。掌握了这些技巧,开发者可以创建出更加美观、互动性强的网页应用,提升用户的满意度和使用效率。希望本文能帮助读者更好地理解和应用这些技术,提升自己的编程能力。
编写第一个油猴脚本是每位开发者迈向自动化和个性化网页体验的第一步。在这个过程中,我们将通过一个具体的例子来展示如何使用油猴脚本进行网页数据抓取和自定义UI交互。
假设我们需要从一个在线论坛中抓取帖子的标题和作者信息,并将其显示在一个自定义的弹出窗口中。首先,我们需要安装油猴脚本管理器,并创建一个新的脚本。在脚本编辑器中,我们可以开始编写我们的JavaScript代码。
// ==UserScript==
// @name 论坛数据抓取
// @namespace http://tampermonkey.net/
// @version 0.1
// @description 从论坛中抓取帖子标题和作者信息
// @author 张晓
// @match https://example-forum.com/*
// @grant GM_xmlhttpRequest
// @grant GM_addStyle
// ==/UserScript==
(function() {
'use strict';
// 获取所有帖子的标题和作者
const posts = document.querySelectorAll('.post');
const data = [];
posts.forEach(post => {
const title = post.querySelector('.title').innerText;
const author = post.querySelector('.author').innerText;
data.push({ title, author });
});
// 创建弹出窗口
const popup = document.createElement('div');
popup.id = 'popup';
popup.style.display = 'none';
popup.style.position = 'fixed';
popup.style.top = '50%';
popup.style.left = '50%';
popup.style.transform = 'translate(-50%, -50%)';
popup.style.background = 'white';
popup.style.padding = '20px';
popup.style.border = '1px solid #ccc';
const closeButton = document.createElement('button');
closeButton.innerText = '关闭';
closeButton.addEventListener('click', () => {
popup.style.display = 'none';
});
popup.appendChild(closeButton);
// 显示数据
data.forEach(item => {
const p = document.createElement('p');
p.innerText = `标题: ${item.title}, 作者: ${item.author}`;
popup.appendChild(p);
});
document.body.appendChild(popup);
// 添加按钮以显示弹出窗口
const button = document.createElement('button');
button.innerText = '显示帖子信息';
button.addEventListener('click', () => {
popup.style.display = 'block';
});
document.body.appendChild(button);
// 添加自定义样式
GM_addStyle(`
#popup {
z-index: 1000;
}
`);
})();
通过上述代码,我们实现了从论坛中抓取帖子的标题和作者信息,并将其显示在一个自定义的弹出窗口中。用户可以通过点击按钮来查看这些信息。这个简单的例子展示了油猴脚本的基本功能和使用方法。
编写完第一个油猴脚本后,调试和优化是确保脚本稳定性和高效性的关键步骤。在调试过程中,我们需要关注以下几个方面:
// 使用console.log记录调试信息
console.log('抓取到的数据:', data);
// 优化DOM操作
const popupContent = data.map(item => `<p>标题: ${item.title}, 作者: ${item.author}</p>`).join('');
popup.innerHTML = popupContent + '<button id="closePopup">关闭</button>';
// 绑定事件
document.getElementById('closePopup').addEventListener('click', () => {
popup.style.display = 'none';
});
通过以上优化,我们可以确保脚本在实际使用中更加稳定和高效。
在编写和使用油猴脚本时,安全性是一个不容忽视的问题。以下是一些重要的安全考虑:
GM_xmlhttpRequest
进行跨域请求。// 转义用户输入
function escapeHtml(unsafe) {
return unsafe
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
}
// 安全地显示数据
const popupContent = data.map(item => `<p>标题: ${escapeHtml(item.title)}, 作者: ${escapeHtml(item.author)}</p>`).join('');
popup.innerHTML = popupContent + '<button id="closePopup">关闭</button>';
通过以上措施,我们可以确保油猴脚本在提供便利的同时,不会对用户的安全造成威胁。希望本文能帮助读者更好地理解和应用油猴脚本,提升自己的编程能力和用户体验。
在实际应用中,许多网页的数据分布在多个页面上,因此,如何高效地处理多页面数据成为了油猴脚本的一个重要课题。多页面处理策略不仅关系到数据抓取的完整性,还直接影响到脚本的性能和用户体验。
最直接的方法是通过循环遍历每个页面,逐个抓取数据。假设我们需要从一个分页的论坛中抓取所有帖子的信息,可以使用以下代码实现:
// ==UserScript==
// @name 论坛多页面数据抓取
// @namespace http://tampermonkey.net/
// @version 0.1
// @description 从论坛中抓取多页面的帖子信息
// @author 张晓
// @match https://example-forum.com/*
// @grant GM_xmlhttpRequest
// @grant GM_addStyle
// ==/UserScript==
(function() {
'use strict';
const totalPages = 5; // 假设总共有5页
const data = [];
function fetchData(page) {
GM_xmlhttpRequest({
method: "GET",
url: `https://example-forum.com/page/${page}`,
onload: function(response) {
const parser = new DOMParser();
const doc = parser.parseFromString(response.responseText, 'text/html');
const posts = doc.querySelectorAll('.post');
posts.forEach(post => {
const title = post.querySelector('.title').innerText;
const author = post.querySelector('.author').innerText;
data.push({ title, author });
});
if (page < totalPages) {
fetchData(page + 1);
} else {
displayData();
}
}
});
}
function displayData() {
const popup = document.createElement('div');
popup.id = 'popup';
popup.style.display = 'none';
popup.style.position = 'fixed';
popup.style.top = '50%';
popup.style.left = '50%';
popup.style.transform = 'translate(-50%, -50%)';
popup.style.background = 'white';
popup.style.padding = '20px';
popup.style.border = '1px solid #ccc';
const closeButton = document.createElement('button');
closeButton.innerText = '关闭';
closeButton.addEventListener('click', () => {
popup.style.display = 'none';
});
popup.appendChild(closeButton);
data.forEach(item => {
const p = document.createElement('p');
p.innerText = `标题: ${item.title}, 作者: ${item.author}`;
popup.appendChild(p);
});
document.body.appendChild(popup);
const button = document.createElement('button');
button.innerText = '显示帖子信息';
button.addEventListener('click', () => {
popup.style.display = 'block';
});
document.body.appendChild(button);
GM_addStyle(`
#popup {
z-index: 1000;
}
`);
}
fetchData(1);
})();
通过递归调用fetchData
函数,我们可以逐页抓取数据,直到所有页面的数据都抓取完毕。这种方法虽然简单,但在处理大量页面时可能会导致性能问题。
为了提高多页面数据抓取的效率,可以采用并行处理的方式。通过同时发送多个请求,可以显著减少总的抓取时间。然而,需要注意的是,并行请求过多可能会对服务器造成压力,甚至导致请求被拒绝。因此,需要合理控制并发数量。
function fetchDataParallel(pages) {
const promises = pages.map(page => {
return new Promise((resolve, reject) => {
GM_xmlhttpRequest({
method: "GET",
url: `https://example-forum.com/page/${page}`,
onload: function(response) {
const parser = new DOMParser();
const doc = parser.parseFromString(response.responseText, 'text/html');
const posts = doc.querySelectorAll('.post');
const pageData = [];
posts.forEach(post => {
const title = post.querySelector('.title').innerText;
const author = post.querySelector('.author').innerText;
pageData.push({ title, author });
});
resolve(pageData);
},
onerror: reject
});
});
});
Promise.all(promises)
.then(results => {
results.forEach(pageData => {
data.push(...pageData);
});
displayData();
})
.catch(error => {
console.error('Error fetching data:', error);
});
}
const pages = Array.from({ length: totalPages }, (_, i) => i + 1);
fetchDataParallel(pages);
通过Promise.all
,我们可以并行发送多个请求,并在所有请求完成后统一处理数据。这种方法不仅提高了抓取效率,还保持了代码的简洁性。
在处理多页面数据时,分页是一个常见的需求。通过合理的分页策略,可以有效地管理和展示大量数据,提升用户体验。
假设我们已经抓取了多页面的数据,现在需要将这些数据分页显示。可以使用以下代码实现分页功能:
const itemsPerPage = 10; // 每页显示10条数据
let currentPage = 1;
function displayPage(page) {
const start = (page - 1) * itemsPerPage;
const end = start + itemsPerPage;
const pageData = data.slice(start, end);
const popup = document.getElementById('popup');
popup.innerHTML = '';
const closeButton = document.createElement('button');
closeButton.innerText = '关闭';
closeButton.addEventListener('click', () => {
popup.style.display = 'none';
});
popup.appendChild(closeButton);
pageData.forEach(item => {
const p = document.createElement('p');
p.innerText = `标题: ${item.title}, 作者: ${item.author}`;
popup.appendChild(p);
});
const prevButton = document.createElement('button');
prevButton.innerText = '上一页';
prevButton.disabled = page === 1;
prevButton.addEventListener('click', () => {
displayPage(page - 1);
});
const nextButton = document.createElement('button');
nextButton.innerText = '下一页';
nextButton.disabled = end >= data.length;
nextButton.addEventListener('click', () => {
displayPage(page + 1);
});
popup.appendChild(prevButton);
popup.appendChild(nextButton);
}
const button = document.createElement('button');
button.innerText = '显示帖子信息';
button.addEventListener('click', () => {
displayPage(currentPage);
document.getElementById('popup').style.display = 'block';
});
document.body.appendChild(button);
通过displayPage
函数,我们可以根据当前页码显示相应范围的数据,并提供上一页和下一页的导航按钮。这样,用户可以方便地浏览多页面的数据。
在某些情况下,我们可能需要对抓取的数据进行汇总和统计。例如,计算某个作者发布的帖子数量,或者统计某个时间段内的帖子数量。可以使用以下代码实现数据汇总:
function summarizeData() {
const summary = {};
data.forEach(item => {
if (!summary[item.author]) {
summary[item.author] = 0;
}
summary[item.author]++;
});
const summaryPopup = document.createElement('div');
summaryPopup.id = 'summaryPopup';
summaryPopup.style.display = 'none';
summaryPopup.style.position = 'fixed';
summaryPopup.style.top = '50%';
summaryPopup.style.left = '50%';
summaryPopup.style.transform = 'translate(-50%, -50%)';
summaryPopup.style.background = 'white';
summaryPopup.style.padding = '20px';
summaryPopup.style.border = '1px solid #ccc';
const closeButton = document.createElement('button');
closeButton.innerText = '关闭';
closeButton.addEventListener('click', () => {
summaryPopup.style.display = 'none';
});
summaryPopup.appendChild(closeButton);
for (const [author, count] of Object.entries(summary)) {
const p = document.createElement('p');
p.innerText = `${author}: ${count}篇帖子`;
summaryPopup.appendChild(p);
}
document.body.appendChild(summaryPopup);
const summaryButton = document.createElement('button');
summaryButton.innerText = '显示数据汇总';
summaryButton.addEventListener('click', () => {
summaryPopup.style.display = 'block';
});
document.body.appendChild(summaryButton);
}
summarizeData();
通过summarizeData
函数,我们可以对抓取的数据进行汇总,并将结果显示在一个新的弹出窗口中。这样,用户可以快速了解数据的分布情况。
在编写油猴脚本时,性能和资源管理是不可忽视的重要方面。高效的脚本不仅能够提升用户体验,还能减少对服务器的压力,确保脚本的稳定运行。
频繁的DOM操作会严重影响页面性能。为了提高性能,可以尽量减少DOM操作的次数。例如,可以一次性生成所有需要的DOM元素,然后再批量插入到页面中。
const popupContent = data.map(item => `<p>标题: ${item.title}, 作者
## 六、油猴脚本在实际应用中的案例分析
### 6.1 案例一:在线新闻网站数据抓取
在当今信息爆炸的时代,新闻网站成为了人们获取最新资讯的主要渠道。然而,面对海量的信息,如何高效地抓取和整理这些数据,成为了许多企业和个人的迫切需求。油猴脚本作为一种强大的用户脚本管理器,可以轻松实现这一目标。
假设我们需要从一个在线新闻网站中抓取最新的新闻标题和摘要,并将其保存到本地文件中。首先,我们需要安装油猴脚本管理器,并创建一个新的脚本。在脚本编辑器中,我们可以开始编写我们的JavaScript代码。
```javascript
// ==UserScript==
// @name 新闻网站数据抓取
// @namespace http://tampermonkey.net/
// @version 0.1
// @description 从新闻网站中抓取新闻标题和摘要
// @author 张晓
// @match https://example-news.com/*
// @grant GM_xmlhttpRequest
// @grant GM_setValue
// @grant GM_getValue
// ==/UserScript==
(function() {
'use strict';
// 获取所有新闻的标题和摘要
const newsItems = document.querySelectorAll('.news-item');
const data = [];
newsItems.forEach(item => {
const title = item.querySelector('.title').innerText;
const summary = item.querySelector('.summary').innerText;
data.push({ title, summary });
});
// 将数据保存到本地
GM_setValue('newsData', JSON.stringify(data));
// 创建一个按钮以显示抓取的数据
const button = document.createElement('button');
button.innerText = '显示新闻数据';
button.addEventListener('click', () => {
const savedData = JSON.parse(GM_getValue('newsData', '[]'));
alert(JSON.stringify(savedData, null, 2));
});
document.body.appendChild(button);
})();
通过上述代码,我们实现了从新闻网站中抓取新闻标题和摘要,并将其保存到本地。用户可以通过点击按钮来查看这些数据。这个简单的例子展示了油猴脚本在数据抓取方面的强大功能。
电商平台是现代购物的重要场所,每天都有大量的商品信息更新。对于商家和消费者来说,及时获取这些信息至关重要。油猴脚本可以帮助我们高效地抓取电商平台的商品信息,如价格、库存和评价等。
假设我们需要从一个电商平台上抓取某类商品的价格和评价信息,并将其显示在一个自定义的弹出窗口中。首先,我们需要安装油猴脚本管理器,并创建一个新的脚本。在脚本编辑器中,我们可以开始编写我们的JavaScript代码。
// ==UserScript==
// @name 电商平台信息爬取
// @namespace http://tampermonkey.net/
// @version 0.1
// @description 从电商平台上抓取商品信息
// @author 张晓
// @match https://example-ecommerce.com/*
// @grant GM_xmlhttpRequest
// @grant GM_addStyle
// ==/UserScript==
(function() {
'use strict';
// 获取所有商品的价格和评价
const products = document.querySelectorAll('.product');
const data = [];
products.forEach(product => {
const price = product.querySelector('.price').innerText;
const rating = product.querySelector('.rating').innerText;
data.push({ price, rating });
});
// 创建弹出窗口
const popup = document.createElement('div');
popup.id = 'popup';
popup.style.display = 'none';
popup.style.position = 'fixed';
popup.style.top = '50%';
popup.style.left = '50%';
popup.style.transform = 'translate(-50%, -50%)';
popup.style.background = 'white';
popup.style.padding = '20px';
popup.style.border = '1px solid #ccc';
const closeButton = document.createElement('button');
closeButton.innerText = '关闭';
closeButton.addEventListener('click', () => {
popup.style.display = 'none';
});
popup.appendChild(closeButton);
// 显示数据
data.forEach(item => {
const p = document.createElement('p');
p.innerText = `价格: ${item.price}, 评价: ${item.rating}`;
popup.appendChild(p);
});
document.body.appendChild(popup);
// 添加按钮以显示弹出窗口
const button = document.createElement('button');
button.innerText = '显示商品信息';
button.addEventListener('click', () => {
popup.style.display = 'block';
});
document.body.appendChild(button);
// 添加自定义样式
GM_addStyle(`
#popup {
z-index: 1000;
}
`);
})();
通过上述代码,我们实现了从电商平台上抓取商品的价格和评价信息,并将其显示在一个自定义的弹出窗口中。用户可以通过点击按钮来查看这些信息。这个例子展示了油猴脚本在处理复杂数据和自定义UI交互方面的强大能力。
社交媒体平台是人们交流和分享的重要场所,每天都有大量的用户生成内容。对于企业和个人来说,监控这些内容,了解用户的意见和反馈,具有重要的意义。油猴脚本可以帮助我们高效地抓取社交媒体上的内容,并进行实时监控。
假设我们需要从一个社交媒体平台上抓取用户的评论和点赞数,并将其显示在一个自定义的弹出窗口中。首先,我们需要安装油猴脚本管理器,并创建一个新的脚本。在脚本编辑器中,我们可以开始编写我们的JavaScript代码。
// ==UserScript==
// @name 社交媒体内容监控
// @namespace http://tampermonkey.net/
// @version 0.1
// @description 从社交媒体上抓取用户评论和点赞数
// @author 张晓
// @match https://example-social.com/*
// @grant GM_xmlhttpRequest
// @grant GM_addStyle
// ==/UserScript==
(function() {
'use strict';
// 获取所有用户的评论和点赞数
const comments = document.querySelectorAll('.comment');
const data = [];
comments.forEach(comment => {
const text = comment.querySelector('.text').innerText;
const likes = comment.querySelector('.likes').innerText;
data.push({ text, likes });
});
// 创建弹出窗口
const popup = document.createElement('div');
popup.id = 'popup';
popup.style.display = 'none';
popup.style.position = 'fixed';
popup.style.top = '50%';
popup.style.left = '50%';
popup.style.transform = 'translate(-50%, -50%)';
popup.style.background = 'white';
popup.style.padding = '20px';
popup.style.border = '1px solid #ccc';
const closeButton = document.createElement('button');
closeButton.innerText = '关闭';
closeButton.addEventListener('click', () => {
popup.style.display = 'none';
});
popup.appendChild(closeButton);
// 显示数据
data.forEach(item => {
const p = document.createElement('p');
p.innerText = `评论: ${item.text}, 点赞数: ${item.likes}`;
popup.appendChild(p);
});
document.body.appendChild(popup);
// 添加按钮以显示弹出窗口
const button = document.createElement('button');
button.innerText = '显示评论信息';
button.addEventListener('click', () => {
popup.style.display = 'block';
});
document.body.appendChild(button);
// 添加自定义样式
GM_addStyle(`
#popup {
z-index: 1000;
}
`);
})();
通过上述代码,我们实现了从社交媒体平台上抓取用户的评论和点赞数,并将其显示在一个自定义的弹出窗口中。用户可以通过点击按钮来查看这些信息。这个例子展示了油猴脚本在处理动态内容和实时监控方面的强大功能。
通过这些具体的案例,我们可以看到油猴脚本在数据抓取和自定义UI交互方面的广泛应用。无论是新闻网站、电商平台还是社交媒体,油猴脚本都能帮助我们高效地获取和处理数据,提升用户体验。希望这些案例能为读者提供实用的参考,激发更多的创新和探索。
{"error":{"code":"invalid_parameter_error","param":null,"message":"Single round file-content exceeds token limit, please use fileid to supply lengthy input.","type":"invalid_request_error"},"id":"chatcmpl-81043299-3054-92bf-b1b9-60ba064060c9"}
本文详细介绍了如何使用油猴(Tampermonkey)脚本进行网页数据抓取和自定义UI交互。通过具体代码示例,读者可以深入理解油猴脚本的工作原理,并掌握如何编写用于数据抓取的JavaScript爬虫。文章涵盖了HTML、CSS和JavaScript的编写技巧,以及如何通过油猴脚本实现多页面数据抓取。此外,本文还提供了多个实际应用案例,包括在线新闻网站数据抓取、电商平台信息爬取和社交媒体内容监控,展示了油猴脚本在不同场景下的强大功能。希望本文能帮助读者彻底掌握油猴脚本的使用方法,提升自己的编程能力和用户体验。同时,强调油猴脚本的使用仅限于学习目的,不得用于不当行为。