【问题标题】:“How to fix ‘NoneType' object is not subscriptable”“如何修复‘NoneType’对象不可下标”
【发布时间】:2019-01-04 07:20:54
【问题描述】:

我正在尝试从 Amazon.com 获取产品信息。

我在互联网上搜索了一些代码,我找到了一个。我试图修复代码中的错误,但我坚持使用下面的错误。下面代码的最后一行似乎有问题,因为我收到了这个错误:'NoneType' object is not subscriptable

for asin in asin_array:

    item_array=[] #An array to store details of a single product.
    amazon_url="https://www.amazon.com/dp/"+asin #The general structure of a url

    response = session.get(amazon_url, headers=headers, verify=False) #get the response

    item_array.append(response.html.search('a-color-price">${}<')[0]) #Extracting the price

【问题讨论】:

  • 将完整的回溯复制粘贴到您的问题中肯定会有所帮助。虽然它看起来像一个索引问题。 response.html.search('a-color-price"&gt;${}&lt;') 返回None?如果是这样,请对其进行一些验证(即 if 语句或诸如此类)。

标签: python arrays append response amazon


【解决方案1】:

因为你的回复是None

基本上,你试试这个

print(None[0])

这是不可能的 检查您的回复或搜索标签可能不可用

【讨论】:

    【解决方案2】:

    你可以在提取之前检查它:

    price_data = response.html.search('a-color-price">${}<')
    if price_data:
        item_array.append(price_data[0])
    

    【讨论】:

      猜你喜欢
      • 2019-09-27
      • 2017-03-11
      • 2021-02-04
      • 1970-01-01
      • 2016-03-30
      • 2015-02-27
      • 2013-09-22
      • 1970-01-01
      相关资源
      最近更新 更多