Possible Duplicate:
convert list to string to insert into my sql in one row in python scrapyUnboundLocalError:分配
我试图写HTML页面直接到MySQL数据库中提取数据之前引用局部变量“项目”。但过去工作的代码不再这样做。有人可以帮我吗?
def parse(self, response):
hxs = HtmlXPathSelector(response)
sites = hxs.select('//ul/li')
con = MySQLdb.connect(
host="localhost",
user="dreamriks",
passwd="dreamriks",
db="scraped_data"
)
cur = con.cursor()
for site in sites:
items = [site.select('//h2').extract()]
item = [site.select('//h3').extract()]
meta = [site.select('//meta').extract()]
for index in range (len(items)): #<-- exception raises here
str = items[index]
cur.execute("""Insert into h2_meta(h2) Values(%s)""",(str))
con.commit()
con.close()
上面的代码提供了以下错误:
exceptions.UnboundLocalError: local variable 'items' referenced before assignment
你需要用更合适的标题提出更好的问题。这个问题对其他人来说几乎没有任何价值,即使这样做,人们会如何寻找它?此外,审查用户名和密码可能是一个好主意。 – nfirvine 2012-02-01 01:57:13