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