首页 > 解决方案 > 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

检查 URL 代码后,我需要从以下类中获取数据product-namelistPrice并且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

解决方案


你想要达到的目标是可能的。只是不在浏览器javascript上。你需要 NodeJS。
您可以使用网页抓取技术来呈现网页并在后端提取标题、价格等,并在 API 中提供这些信息,然后从您的前端发出获取请求。您可以使用 Puppeteer 进行抓取。请查看Puppeteer了解更多信息。


推荐阅读