网站首页 > 技术教程 正文
这里我们介绍两种方法来获取:
- javaScrip获取 因为浏览器安全问题在浏览器中运行时可能会存在跨域问题
- nodejs获取获取不会存在跨域问题
一、JavaScript获取
可以使用 JavaScript 的 XMLHttpRequest 对象或 fetch API 来获取网页的 HTML 代码,然后使用正则表达式或 DOM 操作来提取网页的元数据。
例如,使用 fetch API 获取 HTML 代码并提取 title标题 keywords关键词 description描述 favicon图标 的示例代码如下:
注意:如果网站有跨域无法使用该方法
定义方法:
async function getWebsiteMetadata(url) {
try {
const response = await fetch(url);
const text = await response.text();
const parser = new DOMParser();
const doc = parser.parseFromString(text, 'text/html');
// 获取 title
const title = doc.title;
// 获取 favicon
const faviconEl = doc.querySelector('link[rel~="icon"]');
const favicon = faviconEl ? faviconEl.href : null;
// 获取 keywords
const keywordsEl = doc.querySelector('meta[name~="keywords"]');
const keywords = keywordsEl ? keywordsEl.content : null;
// 获取 description
const descriptionEl = doc.querySelector('meta[name~="description"]');
const description = descriptionEl ? descriptionEl.content : null;
return { title,keywords, description,favicon };
} catch (error) {
console.error('Error fetching website metadata:', error);
return {};
}
}
使用方法:
// 使用函数获取指定网站的元数据
getWebsiteMetadata('https://www.example.com').then(metadata => {
console.log(metadata);
// 请求地址:
// https://juejin.cn/post/7349561234932367401?utm_source=gold_browser_extension
// metadata 返回数据为:
// {
// "title": "Uiverse.io:打造 超炫酷 界面 的秘密武器!!! - 掘金",
// "keywords": "前端,JavaScript,CSS",
// "description": "偶尔闲逛国外网站,会不经意看到一些炫酷的网页元素,不禁呆呆的观察一会,相信你也遇到过吧,特别是新颖炫酷的界面元素,那么我们如何实现这些炫酷的效果的呢? 如果你特别精通 css 的话, 可能这些实现起来",
// "favicon": "https://lf3-cdn-tos.bytescm.com/obj/static/xitu_juejin_web/static/favicons/favicon-32x32.png"
// }
});
二、NODE获取
推荐使用node端获取不会存在跨越问题
node来获取流程通常包括以下几个步骤:
- 发发送网络请求:首先需要发送网络请求,获取网页的HTML代码。Node.js有很多第三方模块可以帮助我们进行网络请求,比如Request、Axios等等。这些模块可以帮助我们发送HTTP请求,并且可以设置请求头、请求参数等等。
- 解析HTML页面:需要解析HTML页面,提取出有用的信息,比如标题、正文、链接等等。在Node.js中,我们可以使用Cheerio模块来解析HTML页面。Cheerio是一个类似于jQuery的库,可以帮助我们更方便地进行HTML解析和DOM操作。
cheerio是第三方模块,所以我们需要先下载安装:
npm install cheerio
在根目录下新建 index.js
// 引入 node 内置模块
const http = require('http');
const https = require('https');
// 引入 cheerio 模块
const cheerio = require('cheerio');
// 定义方法
const getWebsiteMetadata = (url)=> {
// 正则匹配请求是 https 还是 http
const pattern = /^http(s)?:\/\//;
const isHTTPS = pattern.test(url);
const request = isHTTPS ? https : http;
// 使用 Node.js 的内置 http或者https 模块来发起 GET 请求
request.get(url, (resp) => {
let data = '';
// 接收数据
resp.on('data', (chunk) => {
data += chunk;
});
// 数据接收完毕
resp.on('end', () => {
// 使用 cheerio 模块来解析 HTML,将 HTML 转换成一个类似于 jQuery 的对象 $
const $ = cheerio.load(data);
// 获取 title
const title = $('title').text().trim();
// 获取 favicon
const faviconEl = $('link[rel~="icon"]');
const favicon = faviconEl ? faviconEl.eq(0).attr('href') : null;
// 获取 keywords
const keywordsEl = $('meta[name~="keywords"]');
const keywords = keywordsEl ? keywordsEl.eq(0).attr('content') : null;
// 获取 description
const descriptionEl = $('meta[name~="description"]');
const description = descriptionEl ? descriptionEl.eq(0).attr('content') : null;
// 输出结果
const result = { title, favicon, keywords, description }
console.log(result)
});
}).on("error", (err) => {
console.log("Error: " + err.message);
});
}
getWebsiteMetadata('https://juejin.cn/post/7341642966790766627')
getWebsiteMetadata('https://blog.csdn.net/weixin_46232841/article/details/131984150')
上面这段代码使用了 Node.js 的 http 模块来发起 GET 请求,并使用 cheerio 模块来解析 HTML
它首先获取url的 HTML,然后使用 cheerio 将 HTML 转换成一个类似于 jQuery 的对象 $
接着使用 $ 来查找页面中的 title、favicon、keywords、description,并将它们定义对象输出
最后将这个对象输出到控制台上,我们来看看控制台会输出什么:
// 控制台使用node运行 index.js
Maya@192 Desktop % node index.js
{
title: '4 年深度 Mac OS 用户装机必备软件推荐 - 掘金',
favicon: 'https://lf3-cdn-tos.bytescm.com/obj/static/xitu_juejin_web/static/favicons/favicon-32x32.png',
keywords: '人工智能,开源,产品',
description: '每年 Apple官方 都会推出年度App表彰优秀的开发者,作为一枚多年Mac的使用用户,让我种草不少有趣的 App,也让我想要总结Mac OS爱用软件造福大家,无论是工作还是生活都能高频用上,盘点开始'
}
{
title: '都2023年了还不会Node.js爬虫?快学起来!_node爬虫-CSDN博客',
favicon: 'https://g.csdnimg.cn/static/logo/favicon32.ico',
keywords: 'node爬虫',
description: '文章浏览阅读2.1k次,点赞3次,收藏7次。爬虫(Web Crawler)是一种自动化程序,可以在互联网上自动抓取网页,并从中提取有用的信息。爬虫可以模拟人类浏览器的行为,自动访问网站、解析网页、提取数据等。通俗来说,爬虫就像是一只蜘蛛,它会沿着网页上的链接不断爬行,把整个网站的内容都爬取下来。这样,我们就可以从大量的网页中获取到我们需要的数据。动态页面:动态页面是指页面内容是通过 JavaScript 或者其他脚本语言动态生成的页面,这种页面往往需要使用浏览器来执行脚本才能获取到完整的页面内容,因此比较难以爬取。_node爬虫'
}
这样就实现了获取网页的标题、关键字、描述和图标。
猜你喜欢
- 2024-09-25 工具推荐:dismap 快速资产发现和识别工具
- 2024-09-25 谷歌优化Chrome全局媒体控件 调整专辑封面尺寸
- 2024-09-25 分享几个优质的油猴脚本 油猴脚本推荐排行2020
- 2024-09-25 电商平台被入侵,黑客通过图片盗取电商平台用户隐私信息
- 2024-09-25 网站地址前的小图标怎么添加 网站后面加地址后缀
- 2024-09-25 秒开WebView?Android性能优化全攻略
- 2024-09-25 【Python程序开发系列】介绍一款轻量级高自由度web框架-NiceGUI
- 2024-09-25 Zotero文献管理 | 添加文献检索引擎(附下载)
- 2024-09-25 Go 每日一库之 negroni 每日一占
- 2024-09-25 这5个好玩又实用的在线工具,你还不收藏吗?
你 发表评论:
欢迎- 05-1613步震撼淘宝大促闪光裂纹破墙立体字PS制作教程
- 05-16AI教程 | 绘制扁平的萌萌哒图标
- 05-160基础学平面设计所需了解的基础常识汇总
- 05-16自学平面设计需要多长时间?十六年职业设计总监告诉你
- 05-16平面设计都要学习哪些内容?
- 05-16李涛PS教程 高手之路PS教程 合成教程 —制作一个小星球
- 05-16Illustrator实例教程:制作炫酷的漩涡效果
- 05-16Illustrator实例教程:利用混合工具制作一朵炫酷的花
- 最近发表
- 标签列表
-
- sd分区 (65)
- raid5数据恢复 (81)
- 地址转换 (73)
- 手机存储卡根目录 (55)
- tcp端口 (74)
- project server (59)
- 双击ctrl (55)
- 鼠标 单击变双击 (67)
- debugview (59)
- 字符动画 (65)
- flushdns (57)
- ps复制快捷键 (57)
- 清除系统垃圾代码 (58)
- web服务器的架设 (67)
- 16进制转换 (69)
- xclient (55)
- ps源文件 (67)
- filezilla server (59)
- 句柄无效 (56)
- word页眉页脚设置 (59)
- ansys实例 (56)
- 6 1 3固件 (59)
- sqlserver2000挂起 (59)
- vm虚拟主机 (55)
- config (61)
本文暂时没有评论,来添加一个吧(●'◡'●)