Problems Generating A List In Python And Storing It In A Mysql Database
Possible Duplicate: convert list to string to insert into my sql in one row in python scrapy I have written a script to insert data into mysql as shown below, but it is insertin
Solution 1:
It looks like you are overwriting your lists instead of appending to them when you do:
for site in sites:
items = [site.select('//h2').extract()]
item = [site.select('//h3').extract()]
meta = [site.select('//meta').extract()]
I think you might want to change it to:
for site in sites:
items.append(site.select('//h2').extract())
item.append(site.select('//h3').extract())
meta.append(site.select('//meta').extract())
Solution 2:
This
site.select('//h2').extract()
will return a list of all the 'h2' tags in that particular site. After appending this lists, you will need to iterate over each element in the 'items' list as they all contain a list. The second iteration should look like this.
for elem inrange( len( items )):
for index inrange( len(elem) ):
str = elem[index]
cur.execute("""Insert into h2_meta(h2) Values (%s)""",(str))
Also avoid using keywords like 'str' as variable names, and declare each list before appending to it.
Post a Comment for "Problems Generating A List In Python And Storing It In A Mysql Database"