简体   繁体   English

Scrapy TypeError:列表索引必须是整数或切片,而不是 str

[英]Scrapy TypeError: list indices must be integers or slices, not str

I want to do increment on each itration of for loop but i get the TypeError我想对 for 循环的每次迭代进行增量,但我得到了TypeError

version['version_code'][i] = version_code version['version_code'][i] = version_code
TypeError: list indices must be integers or slices, not str类型错误:列表索引必须是整数或切片,而不是 str

index = 0
version = VersionsItem()
version = []
for rRow in releaseRows:

    #rRow is a string

    releasehref = rRow.xpath(".//a/@href").get()
    if releasehref:
        exp = releasehref.replace("/apk/","")
        exp = exp.split("/")
        Vslug = exp[2]
        app_slug = exp[1]
        # #l-speed-root-v2-0-9
        expr = exp[2].replace("-release","")
        expr = expr.split(app_slug+"-",1)[1]
        version_code = expr.replace("-","")
        version_param = expr.replace("-",".")

        version['version_code'][index] = version_code
        version['version_param'][index] = version_param
        version['Vslug'][index] = Vslug
        index += 1

3rd line of your code reassigns version to be a list not a VersionsItem() object any more after the second line.代码的第三行在第二行之后将 version 重新分配为列表而不是 VersionsItem() 对象。 Since it is a list now you can no longer access it with strings as you did in this part由于它现在是一个列表,因此您不能再像本部分那样使用字符串访问它

    version['version_code'][index] = version_code
    version['version_param'][index] = version_param
    version['Vslug'][index] = Vslug
    index += 1

(which I assume you can do with the VersionsItem() object but you did not provide that code for us to analyze). (我假设您可以使用 VersionsItem() 对象执行此操作,但您没有提供该代码供我们分析)。

Another problem that I notice is that you are using an index at as the counter for your loop.我注意到的另一个问题是您使用索引 at 作为循环的计数器。 This is non-pythonic and you should instead use enumerate instead to access a list.这是非 Pythonic 的,您应该改为使用 enumerate 来访问列表。

And one last thing, if you're just trying to extend the list to store more data, then you don't even need an index.最后一件事,如果您只是想扩展列表以存储更多数据,那么您甚至不需要索引。 You can simply append the data to the end of the list and it will automatically add a new element.您可以简单地将数据附加到列表的末尾,它会自动添加一个新元素。 Since you don't need an index then you also don't need to enumerate the for loop like I was talking about before.由于您不需要索引,因此您也不需要像我之前所说的那样枚举 for 循环。 Here is this implemented这是实现的

With this being said, a way of getting your code to work would be to instantiate a dictionary as such:话虽如此,让您的代码工作的一种方法是实例化一个字典:

        #version = VersionsItem() # Old Code
        #version = [] # Old Code
        version = {} # New Code

        # Instantiate all of these elements of the dictionary as being lists
        version['version_code'] = [] # New Code
        version['version_param'] = [] # New Code
        version['Vslug'] = [] # New Code

        for rRow in releaseRows:
        # Enumerated for loop if needed
        #for index, rRow in enumerate(releaseRows):

            #rRow is a string

            releasehref = rRow.xpath(".//a/@href").get()
            if releasehref:
                exp = releasehref.replace("/apk/","")
                exp = exp.split("/")
                Vslug = exp[2]
                app_slug = exp[1]
                # #l-speed-root-v2-0-9
                expr = exp[2].replace("-release","")
                expr = expr.split(app_slug+"-",1)[1]
                version_code = expr.replace("-","")
                version_param = expr.replace("-",".")

                version['version_code'].append(version_code) # New Code
                version['version_param'][index].append(version_param) # New Code
                version['Vslug'][index].append(Vslug) # New Code

This issue appears to be caused not by the i, but by the 'version_code' index.此问题似乎不是由 i 引起的,而是由“version_code”索引引起的。 i is an int and the error pertains to the fact that it got a string rather than an int, indicating that asking for item i is okay, but asking for item 'version code' is no good. i 是一个 int 并且错误与它得到的是一个字符串而不是一个 int 的事实有关,这表明要求项目 i 是可以的,但要求项目“版本代码”是不好的。

I don't know a lot about scrapy, but I would double check what the variable version looks like by putting a print(version) before the line in question.我对scrapy了解不多,但我会通过在相关行之前放置一个print(version)来仔细检查变量版本的样子。

Yeah i have find a solution like:是的,我找到了一个解决方案,例如:

version = {} #NEW CODE

        releaseRows = response.xpath("//div[@id='primary']/div[@class='listWidget']/div[@class='appRow']/div/div[2]/div/h5")
        if releaseRows:
             #NEW CODE
            for index, rRow in enumerate(releaseRows,1):
                releasehref = rRow.xpath(".//a/@href").get()
                if releasehref:
                    exp = releasehref.replace("/apk/","")
                    exp = exp.split("/")
                    Vslug = exp[2]
                    app_slug = exp[1]
                    # #l-speed-root-v2-0-9
                    expr = exp[2].replace("-release","")
                    expr = expr.split(app_slug+"-",1)[1]
                    version_code = expr.replace("-","")
                    version_param = expr.replace("-",".")
                    version[index] = {} #NEW CODE
                    version[index]['version_code'] = version_code #NEW CODE
                    version[index]['version_param'] = version_param #NEW CODE
                    version[index]['Vslug'] = Vslug #NEW CODE

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “TypeError:list indices必须是整数或切片,而不是str” - “TypeError: list indices must be integers or slices, not str” TypeError:列表索引必须是整数或切片,而不是 str - TypeError: List indices must be integers or slices and not str 类型错误:列表索引必须是整数或切片,而不是 str - TypeError: list indices must be integers or slices, not str “TypeError:列表索引必须是整数或切片,而不是 str” - "TypeError: list indices must be integers or slices, not str" TypeError:列表索引必须是整数或切片而不是 str - TypeError: list indices must be integers or slices not str 类型错误:列表索引必须是整数或切片,而不是 str - TypeError: list indices must be integers or slices, not str 抓取数据保存到mongoDB TypeError:列表索引必须是整数或切片,而不是str - Scrapy save data to mongoDB TypeError: list indices must be integers or slices, not str Python3-TypeError:列表索引必须是整数或切片,而不是str-List - Python3 - TypeError: list indices must be integers or slices, not str - List 使用 Python TypeError 解析 JSON:列表索引必须是整数或切片,而不是 str - Parsing JSON with Python TypeError: list indices must be integers or slices, not str 如何解决“类型错误:列表索引必须是整数或切片,而不是 str” - How to solve "TypeError: list indices must be integers or slices, not str"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM