0

我有一个 HTML 页面

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 中,这样我就不必像现在这样手动输入数据了!

有没有人知道要这样做,因为我现在真的被困住了。

谢谢

4

1 回答 1

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()
于 2013-06-28T16:31:29.887 回答