正在获取TypeError:“ NoneType”对象在Web抄袭中无法下标

时间:2019-10-15 09:17:15

标签: python web-scraping

我正在尝试解析网站并将其内容保存到CSV文件中,但在线上

brand = make_rating_sp[0].img["title"].title()

我收到打字稿错误

for container in containers:
   make_rating_sp = container.div.select("a")

   brand = make_rating_sp[0].img["title"].title()

   product_name = container.div.select("a")[2].text

   shipping = container.findAll("li", {"class": "price-ship"})[0].text.strip().replace("$", 
   "").replace(" Shipping", "")
   print("brand: " + brand + "\n")
   print("product_name: " + product_name + "\n")
   print("shipping: " + shipping + "\n")
   f.write(brand + ", " + product_name.replace(",", "|") + ", " + shipping + "\n")

1 个答案:

答案 0 :(得分:0)

您最有可能看到IndexError: list index out of range,但是如果make_rating_sp列表包含元素,但是指定索引处的特定a标签不包含{{ 1}}标签。

尝试添加以下内容:

img

通常在Python中,您会想捕获特定的exceptions,但是在这种情况下,通用的解决方案可能就足够了。