如何获取 Pinterest 高清原图

如何获取 Pinterest 高清原图

logo 辉小志
Pinterest图片下载高清原图TypeScript开源工具

教你如何通过编程方式获取 Pinterest 高清原图,并提供开源工具和在线工具。

Pinterest 是一个基于图片的社交网络,用户可以上传、分享和发现各种图片和视频。我在 pinterest 上发现了好多美图令人爱不释手,但是可能你也发现了,无论是网页端还是移动端,使用 pinterest 自带的图片下载功能,只能下载压缩后的图片,无法获取高清原图。

通过我翻阅 github,发现其实可以通过编程的方式轻松获取到高清原图,核心原理是访问图片页面,在其 html 源码中找到以 https://i.pinimg.com/originals/ 开头的第一个链接,就肯定是图片原图了。

以下为 TypeScript 代码实现:

export async function getPinImageByUrl(
	url: string,
	headers: Record<string, string>
): Promise<string | undefined> {
	try {
		// 发送 HTTP 请求
		const response = await fetch(url, { headers });
		if (!response.ok) {
			throw new Error(`HTTP error! status: ${response.status}`);
		}
		const htmlContent = await response.text();
		// 解析 HTML
		const $ = cheerio.load(htmlContent);
		const scripts = $('script')
			.map((i, el) => $(el).text())
			.get()
			.join('\n');
		// console.log('所有 script 标签的内容:', scripts);

		// 使用正则表达式,寻找 https://i.pinimg.com/originals/... 的 url,只需要第一个,返回
		const match = /https://i.pinimg.com/originals/[^s'"]+/i.exec(scripts);
		if (match) {
			// console.log('匹配到的图片 URL:', match[0]);
			return match[0]; // 返回匹配到的 URL
		}
	} catch (error: any) {
		console.error(`get pin info 失败: ${error.message}`);
		throw error;
	}
}

为了使其更易用,我还做了个小页面来实现这个功能,只要输入 pin 图链接,就可自动获取高清原图了,你可以自己部署,已经开源到 github,点此访问

你可能会说,我不想自己部署,有没有在线工具让我直接使用呢?有的兄弟,有的,我把这个页面放到了我的这个网站,直接打开就能使用了,点此访问

没有兄弟,没有,我的服务器是国内的,竟无法直接访问 Pinterest 服务器,因此无法正确解析😅,太尴尬了,你们还是自己部署使用吧。

有的兄弟,有的,我通过 cloudflare 中转最终还是实现了这个功能,点此访问吧

评论

发表评论

加载评论中...