首页 > 解决方案 > 局部变量未定义 [已解决]

问题描述

        for x in results:
            sponsor = x.findAll(class_="s-sponsored-label-info-icon")

            if sponsor == []:

                for each in results:

                    price_data = {
                        'Item Name': each.h2.a.text.strip(),
                        'Item Price': "Not Available",
                        'Discounted Price': "Not Available",
                        'Percentage of discount': 0.0,
                        'Item rating': each.i if each.i is not None else "No rating",
                        'Url': each.find(class_="a-link-normal a-text-normal").get('href')
                    }

                    # To get the types of prices realprice = non discount \ disprice = discounted price
                    hasdisprice = each.find(class_="a-price")
                    hasrealprice = each.find(class_="a-price a-text-price")

                    if hasrealprice is not None and hasdisprice is not None:
                        price_data["Item Price"] = hasrealprice.find(class_="a-offscreen").text.strip()
                        price_data["Discounted Price"] = hasdisprice.find(class_="a-offscreen").text.strip()
                        ogpriceclean = price_data["Item Price"].replace("s$", "")
                        discountpriceclean = price_data["Discounted Price"].replace("s$", "")
                        ogpriceclean = float(ogpriceclean)
                        discountpriceclean = float(discountpriceclean)

                        # Finding discounted percentage
                        price_data["Percentage of discount"] = findpercentage(ogpriceclean, discountpriceclean)
                    elif hasdisprice is None:  # We know hasrealprice is not None
                        price_data["Item Price"] = hasrealprice.find(class_="a-offscreen").text.strip()
                    elif hasrealprice is None:  # We know hasdisprice is not None
                        price_data["Discounted Price"] = hasdisprice.find(class_="a-offscreen").text.strip()
                    else:  # We know both are None
                        pass  # We could assign something here if needed in this case

                    # print(f"Price data={price_data}")  # Uncomment to make sure it looks right

                    df = df.append(pd.DataFrame(price_data, index=[0]), ignore_index=True)


        return soup

我在运行此程序时遇到问题,这会引发此错误:

此错误表明我在调试后返回无

如果我在没有调试器的情况下运行它,将出现以下错误 ValueError: could not broadcast input array from shape (0,) into shape (1,)

我不知道是什么导致了这个错误,或者如何清理这个代码。

标签: pythonweb-scraping

解决方案


虽然您没有引用错误出现的行号,但它可能是最后一行:

df = df.append(pd.DataFrame({'Item Name': name, 'Item Price': itemprice, 'Discounted Price': disitemprice,'Percentage of discount': discountpercentage, 'Item rating': itemrating, 'Url': urlitem}, index=[0]), ignore_index=True)`

在这里您分配discountpercentage,但这可能尚未定义,特别是因为您的 if/else 情况:

if hasrealprice is not None and hasdisprice is not None:
    ...
    # discountpercentage gets assigned here
else:
    if hasrealprices is not None:
        ...
        # No assignment here
    else:
        ...
        # or here
    if hasdisprice is not None:
        ...
        # or here
    else:
        ...
        # or here

所以,这里的部分问题是你的结构有点要求大量的复制粘贴布线。我建议从最后一行中需要使用的对象开始,特别是字典,然后填写:

for result in results:
    price_data = {
      'Item Name': result.h2.a.text.strip(), 
      'Item Price': "Not Available", 
      'Discounted Price': "Not Available",
      'Percentage of discount': 0.0, 
      'Item rating': each.i if each.i is not None else "No rating", 
      'Url': each.find(class_="a-link-normal a-text-normal").get('href')
    }

    # To get the types of prices realprice = non discount \ disprice = discounted price
    hasdisprice = each.find(class_="a-price")
    hasrealprice = each.find(class_="a-price a-text-price")

    if hasrealprice is not None and hasdisprice is not None:
        price_data["Item Price"] = hasrealprice.find(class_="a-offscreen").text.strip()
        price_data["Discounted Price"] = hasdisprice.find(class_="a-offscreen").text.strip()
        ogpriceclean = float(price_data["Item Price"].replace("s$",""))
        discountpriceclean = float(price_data["Discounted Price"].replace("s$",""))

        # Finding discounted percentage
        price_data["Percentage of discount"] = findpercentage(ogpriceclean, discountpriceclean)
    elif hasdisprice is None:  # We know hasrealprice is not None
        price_data["Item Price"] = hasrealprice.find(class_="a-offscreen").text.strip()
    elif hasrealprice is None:  # We know hasdisprice is not None
        price_data["Discounted Price"] = hasdisprice.find(class_="a-offscreen").text.strip()
    else:  # We know both are None
        pass  # We could assign something here if needed in this case

    # print(f"Price data={price_data}")  # Uncomment to make sure it looks right

    df = df.append(pd.DataFrame(price_data, index=[0]), ignore_index=True)

推荐阅读