2013-05-31 29 views
0

我嘗試使用python和數據庫來顯示一系列信息。但是,我的輸出僅顯示最後一列。 我知道我不是很清楚地表達我的意思。 於是,我把我的代碼和輸出如下: 現在的輸出顯示:Python:如何將一系列數據插入到數據庫中並將它們全部顯示出來

$ python pricewatch.py 
Harvey Norman Site Search 
iPad 2 Wi-Fi 16GB Black 
iPad 2 Wi-Fi 16GB 
iPad mini Wi-Fi + Cellular 32GB 
iPad mini Wi-Fi 16GB 
iPad mini Wi-Fi + Cellular 64GB 
iPad Wi-Fi 64GB with Retina Display 
iPad Wi-Fi 32GB with Retina Display 
iPad 2 Wi-Fi 16GB White 
iPad 2 Wi-Fi + 3G 16GB 
iPad Wi-Fi + Cellular 32GB with Retina Display 
iPad mini Wi-Fi + Cellular 16GB 
$357 
$697 
$756 
$647 
$395 
$545 
$777 
$487 
(8, u'iPad mini Wi-Fi + Cellular 16GB', u'Harvey Norman Site Search', u'$487') 

//你可以看到,它只是顯示最後一個 我的代碼是

url="http://m.harveynorman.com.au/computers/tablets-readers/ipads" 
page=urllib2.urlopen(url) 
soup = BeautifulSoup(page.read()) 

sitename=soup.find('label',{'for':'search'}) 
print sitename.renderContents() 

productname=soup.findAll('strong',{'class':'name fn'}) 
for eachproductname in productname: 

    print eachproductname.renderContents() 

productprice=soup.findAll('span',{'class':'price'}) 
for eachproductprice in productprice: 

    print eachproductprice.renderContents().replace("<span>","").replace("</span>","") 

conn =sqlite3.connect('pricewatch.db') 
c = conn.cursor() 

c.execute("CREATE TABLE if not exists table1 (id integer, name text, store text, price real)") 
eachname = eachproductname.renderContents() 
eachprice = eachproductprice.renderContents().replace("<span>","").replace("</span>","") 
sitename = sitename.renderContents() 
assignnumber = randint(1,30) #issue here,want to assign a series of number by the scriptself 
data = [(assignnumber,eachname,sitename,eachprice), 
     ] 
c.executemany('INSERT INTO table1 VALUES (?,?,?,?)',data) 

#output 
for row in c.execute('select * from table1'): 
     print row 

現在我從數據庫所需的輸出類似 (1,ipadXX,HN,$ 199頁) (2,iPad的XX,NH,200 $) .....

希望任何人都可以給予提示或編輯我的小號CRIPT。

問候 餘杭

回答

1

你應該叫fetchallexecute

for row in c.execute("select * from table1").fetchall(): 
    print(row) 
+0

@PailWoolcock感謝,我用ftechall之前,沒有奏效。 –

3

有什麼不妥:

for row in c.execute('select * from table1'): 
    print row 

只要確保你提交插入第一:

c.executemany('INSERT INTO table1 VALUES (?,?,?,?)',data) 
conn.commit() 

然後,您仍然可以逐個提取記錄,而不是像fetchall()一樣檢索所有行。

+0

嗨,我也嘗試過conn.commit()之前,它只是一次又一次地重複列。不工作,謝謝〜 –

+0

@YuhangZhou然後'數據'是不是你想象的那樣...打印出'數據'... –

+0

嗨,我確定的數據是從上面的腳本刮網站。你的意思是「數據是不是我認爲它是什麼?」〜謝謝 –

0

@喬恩克萊門特是正確的,你的問題就在這裏:

data = [(assignnumber,eachname,sitename,eachprice), 
    ] 

需要在一個循環 - 現在它只能被分配在第一組值,這是所有正在插入。

編輯

好了,你想是這樣的:

for (name, price) in zip(productname, productprice): 
    name_data = name.renderContents() 
    price_data = price.renderContents().replace() # fill in your replacements here 
    site_data = sitename.renderContents() 
    assign_number = random.randint(1,30) 

    c.execute('insert into table1 values (?,?,?,?)', 
      (name_data, price_data, site_data, assign_name)) 
+0

感謝您的這個提示,我會嘗試 –

+0

嗨,我累了添加一個循環,我用了一會兒。不過,我不確定如何在這種情況下循環。我厭倦了循環行時,輸出什麼也沒有顯示;然後我厭倦循環數據,輸出顯示語法錯誤。所以你可以給這個循環提供更多的細節提示 –

相關問題