2013-06-28 99 views
0

我有一個HTML頁面閱讀HTML表和輸入數據到MySQL數據庫

table.html 
<table border="1"> 
<tr> 
<td>row 1, cell 1</td> 
<td>row 1, cell 2</td> 
</tr> 
<tr> 
<td>row 2, cell 1</td> 
<td>row 2, cell 2</td> 
</tr> 
</table> 

我希望能夠lynx -dump這個頁面,從HTML表中的數據插入到mysql數據庫(HTML網頁將總是有相同的標題,但數據將每天更改。

我想要得到這個腳本,然後添加到一個cron,所以我不必手動輸入數據,因爲我在這一刻!

無論如何,有誰知道這樣做,因爲我真的被困在此時此刻。

謝謝

回答

0

我不知道任何現成的解決方案。如果你不怕一些Python編碼,我認爲使用BeautifulSoup在你的html中導航(本身不是這麼簡單的任務)會很容易。

你會碰到這樣的:

from bs4 import BeautifulSoup 
import MySQLdb 
db=MySQLdb.connect(passwd="xxx",db="xxx") 
c=db.cursor() 

soup = BeautifulSoup(html_file) 

tr_list=soup.find_all("tr") 
for tr in tr_list: 
    cell1=tr.find_all("td")[0] 
    cell2=tr.find_all("td")[1] 
    #do your sql insert here 
    c.execute ("SQL query here") 
c.close()