【问题标题】:Python Beautifulsoup Request variantsPython Beautifulsoup 请求变体
【发布时间】:2021-09-15 15:42:38
【问题描述】:

我用 stackoverflow 解决了很多问题。

今天,我决定问我的问题。 我是新学 Python。我正在研究如何从网络上抓取数据。

我来到了一个网站示例,其中的产品在尺寸或颜色方面可能有不同的变体。 我不知道如何“关注”链接以到达变体页面。我可以看到有一个函数调用,但我不知道如何访问这个函数/链接。

以下网址为例:

variant of colour and size

variant of colour:

这是我用来获取所有可用变体的代码,但它没有按我的意愿工作,最后我不知道如何获取链接:

# Define the part of the page I'm interested in:
article = soup.find('header', class_='pdp-header')

for variant in article.find_all('p', class_='pdp-size-variants__title'): 
    print(variant)
    if "Colour" in variant:
        for colours in article.find('div', class_='swatches__item'):
            print(colours)
    if "Size" in variant:
        for sizes in article.find('button', class_='btn-supportive'):
            print(sizes)

我得到的结果是:

<p class="pdp-size-variants__title small--xs mb-1"><strong>Colour</strong></p>
<p class="pdp-size-variants__title small--xs mb-1"><strong>Size</strong></p>

如果你能把我引向正确的方向那就太好了。

非常感谢。

大卫

【问题讨论】:

    标签: python web-scraping beautifulsoup request


    【解决方案1】:

    我可以看到有一个函数调用,但我不知道如何访问这个函数/链接。

    第一步是查看该函数。从看样片开始:

    <div class="swatches__item"><span title="Black" onclick="getProductVariantFunc(12145,267721)" class="swatch active" style="background-image:url('https://ccshop.sirv.com/ccs/images/swatches/Black.png');background-size:100%"> </span></div>
    

    在这里我们看到有一个名为getProductVariantFunconclick 处理程序。现在您应该打开页面源代码并找到该函数的代码:

    <script>function getProductVariantFunc(n, t) {
        $("#product-variants").addClass("disabled-div");
        t !== 0 ? $.ajax({
            cache: !1,
            url: "/VariantAttributes/GetProductHtmlByAttributes",
            type: "POST",
            data: {attributeId: t, productId: n},
            success: function (n) {
                $("#product-info").html(n);
                $("#product-variants").removeClass("disabled-div");
                initProductDetails();
                initPdp();
                renderRecommendedProductList()
            }
        }) : $.ajax({
            cache: !1,
            url: "/VariantAttributes/GetProductHtml",
            type: "POST",
            data: {productId: n},
            success: function (n) {
                $("#product-info").html(n);
                $("#product-variants").removeClass("disabled-div");
                initProductDetails();
                initPdp();
                renderRecommendedProductList()
            }
        })
    }</script>
    

    我们可以在此处看到该页面对 URL "/VariantAttributes/GetProductHtmlByAttributes" 进行了 ajax() 调用。所以你所要做的就是请求相同的 URL 来查看它是否有你需要的数据。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-02-21
      • 2019-02-03
      • 2020-02-11
      • 2020-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多