python - 局部变量未定义 [已解决]
问题描述
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,)
我不知道是什么导致了这个错误,或者如何清理这个代码。
解决方案
虽然您没有引用错误出现的行号,但它可能是最后一行:
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)
推荐阅读
- java - 使用 apache poi 创建 ms word 文档时出现重复的页眉/页脚问题
- sql - 选择期间保持锁定
- r - 将字符串函数应用于绘图中的(所有)实验室类型标签
- c# - 非常简单的抽卡游戏(消除重复+重置数组)
- ansible - Ansible - 加载块时遇到格式错误的块
- kubernetes - 离线检查 Kubernetes 的 YAML 文件的工具
- cordova - 在 IOS 设备中的 ionic 4 本地通知上捕获点击事件
- django - Http 响应 200 1033
- python - 使用 Matplotlib 在圆环图上显示百分比
- python - Python 和 Telethon:如何处理 sw 分发