Shahzaib Chadhar
Shahzaib Chadhar

Reputation: 327

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

version['version_code'][i] = version_code
TypeError: list indices must be integers or slices, not 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

Upvotes: 0

Views: 471

Answers (3)

Shahzaib Chadhar
Shahzaib Chadhar

Reputation: 327

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

Upvotes: 0

zicameau
zicameau

Reputation: 139

3rd line of your code reassigns version to be a list not a VersionsItem() object any more after the second line. 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).

Another problem that I notice is that you are using an index at as the counter for your loop. This is non-pythonic and you should instead use enumerate instead to access a list.

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. 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

Upvotes: 1

d_v_t_
d_v_t_

Reputation: 56

This issue appears to be caused not by the i, but by the 'version_code' index. 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 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.

Upvotes: 0

Related Questions