Skip to content Skip to sidebar Skip to footer

Unboundlocalerror: Local Variable 'items' Referenced Before Assignment

Possible Duplicate: convert list to string to insert into my sql in one row in python scrapy I am trying to write the data extracted from HTML pages directly to a MySQL database

Solution 1:

It looks like you have an indenting problem. The for index in range(len(items)) is part of the for site in sites loop, right?

for site in sites:
        items = [site.select('//h2').extract()]
        item = [site.select('//h3').extract()]
        meta = [site.select('//meta').extract()]
        for index inrange (len( items)):             # <-- make sure this has samestr = items[index]                       # level of indenting as previous lines
             cur.execute("""Insert into h2_meta(h2) Values(%s)""",(str))

Solution 2:

If for site in sites: doesn't iterate because sites is empty, then items never gets initialised.

When you reference it in for index in range (len( items)): the code is assuming that items has been intialised.

Post a Comment for "Unboundlocalerror: Local Variable 'items' Referenced Before Assignment"