javascript - javascript - 从网页中获取产品的标题、价格和颜色
问题描述
我想要什么:从下面的 URL,我想抓取:Title
, Was Price
, Now Price
,all colors aviable
https://www.michaelkors.com/mercer-gallery-medium-logo-tote-bag/_/R-US_30F1GZ5T6B?color=2170
- 标题 = Mercer Gallery 中号徽标托特包
- 价格 = $398.00
- 现在价格 = $149.00
- 颜色 = BRN/SFTPINK、BRN/ACORN
检查 URL 代码后,我需要从以下类中获取数据product-name
,listPrice
并且salePrice
错误:从来源“https://”访问“https://www.michaelkors.com/greenwich-extra-small-studded-patent-leather-crossbody-bag/_/R-US_32F1GGRC5L?color=2711”处的 XMLHttpRequest localhost:44329' 已被 CORS 策略阻止:请求的资源上不存在“Access-Control-Allow-Origin”标头。
我试过的:
var url = 'https://www.michaelkors.com/mercer-gallery-medium-logo-tote-bag/_/R-US_30F1GZ5T6B?color=2170';
var TitleClass = 'product-name';
fetch(url)
.then(res => res.text())
.then(text => {
document.getElementById('DisplayWebCode').innerHTML = text;
console.log(text);
})
.catch(err => console.log(err));
<div id="DisplayWebCode">
</div>
解决方案
你想要达到的目标是可能的。只是不在浏览器javascript上。你需要 NodeJS。
您可以使用网页抓取技术来呈现网页并在后端提取标题、价格等,并在 API 中提供这些信息,然后从您的前端发出获取请求。您可以使用 Puppeteer 进行抓取。请查看Puppeteer了解更多信息。
推荐阅读
- ios - 我想在 iOS 应用程序中显示月份和年份。如何切换月份?
- angular - 在网格中更改 Kendo CheckBox 的样式
- asp.net-mvc - 无法再创建任何视图,“值 -1 超出 [0,2147483647] 的可接受范围”错误
- c# - 使用 Entity Framework Core/ASP.NET Core 进行数据库加密
- android - 应用插件: 'com.google.ar.sceneform.plugin' 。无法建造
- compiler-errors - 基本 ocaml 程序未编译
- microsoft-graph-api - 如何通过单个 POST 请求将带有 GraphServiceClient 的小文件和元数据上传到 OneDrive?
- image - 新的docker镜像如何有依赖的子镜像
- javascript - 从数组元素创建 js 语句
- html - 当输入名称包含数组时如何使用浏览器自动完成?