我学习数据库,我的代码没有返回任何错误但它只写第一行。你能帮助我吗?顺便说一下,链接只是一个实验:D 代码: page = urllib2.urlopen(" https://en.wikipedia.org/wiki/Wushu_at_the_2014_Asian_Games_%E2%80%93_Men%27s_sanda_60_kg") 汤= BeautifulSoup(页面," lxml")
table = soup.find("table", class_="wikitable")
A = []
B = []
C = []
try:
conn = psycopg2.connect("dbname='testdb' user='postgres' host='localhost' password='root'")
except:
print 'Unable to connect to the database.'
cur = conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
for row in table.findAll("tr"):
cells = row.findAll("th")
if len(cells) != 0:
A.append(cells[0].find(text=True))
B.append(cells[1].find(text=True))
C.append(cells[2].find(text=True))
cur.execute("""INSERT INTO test(event_date,event_time,event_event)
VALUES(%s,%s,%s)""", (A,B,C))
conn.commit()
答案 0 :(得分:0)
确保你的
if len(cells) != 0:
条件适用于您的期望。
你也应该致电
del A[:]
del B[:]
del C[:]
在conn.commit()
行之后