1
我需要你的建議和幫助。Python SQLite3插入返回沒有錯誤,但表中沒有數據
我正在編寫一個代碼來解析區域的名稱和從某個網站的相應區域的鏈接。之後,我想將區域的名稱和鏈接存儲在數據庫中(sqlite3)。數據庫已創建,表已創建,但數據無法插入到表中。我嘗試了一些試驗和錯誤,但都沒有奏效。所以,我做了這個線程。
這裏是我的代碼:
'''
usage python capstonePy.py http://www.liverpoolfc.com/fans/lfc-official-supporters-clubs
URL: http://www.liverpoolfc.com/fans/lfc-official-supporters-clubs
Official supporters URL pattern:
http://www.liverpoolfc.com/fans/lfc-official-supporters-clubs/[region]
'''
from sys import argv
from os.path import exists
from BeautifulSoup import *
import urllib
import re
import sqlite3
class FindSupporters:
def __init__(self, *args, **kwargs):
#parsing the url from the command line
url = argv[1]
#make a new database
cur = new_db('liverpudlian.sqlite3')
#open and read the url
fhand = open_and_read(url)
#print how many characters have been retrieved
suc_ret(len(fhand))
#make a list of links (href)
linklst = find_link(fhand)
#make a list of supporters regions
offsuplinklst = fans_link(linklst)
#make a new table and insert the data
officialsup_table(cur, offsuplinklst, 'liverpudlian.sqlite3')
sqlite3.connect('liverpudlian.sqlite3').close()
def new_db(name):
conn = sqlite3.connect(name)
cur = conn.cursor()
return cur
def open_and_read(url):
try:
fhand = urllib.urlopen(url).read()
except:
print '\n'
print "+------------------------------------------------------------------------------+"
print "|\t\t\t\tError: URL not found.\t\t\t\t|"
print "+------------------------------------------------------------------------------+"
print '\n'
quit()
return fhand
def suc_ret(length):
print '\n'
print "+------------------------------------------------------------------------------+"
print "|\t\t", length, "characters have been successfully retrieved\t\t|"
print "+------------------------------------------------------------------------------+"
print '\n'
def find_link(fhand):
links = []
tags = []
soup = BeautifulSoup(fhand)
tags = soup('a')
for tag in tags:
tag = tag.get('href',None)
if tag is not None :
links.append(tag)
return links
def fans_link(linklst):
offsuplinklst = []
for link in linklst:
link = str(link)
link = link.rstrip()
fans = re.findall('.*fans/.+clubs/(.+)', link)
if len(fans) > 0:
offsuplinklst.append(fans[0])
return offsuplinklst
def officialsup_table(cur, offsuplinklst, name):
cur.execute('''
create table if not exists OfficialSup
(ID integer primary key,
Region text unique,
Link text unique,
Retrieved integer)''')
cur.execute('select Region from OfficialSup where Retrieved = 1 limit 1')
try :
cur.fetchone()[0]'
except :
for i in range(len(offsuplinklst)):
reg = offsuplinklst[i]
link = 'http://www.liverpoolfc.com/fans/lfc-official-supporters-clubs/'+offsuplinklst[i]
cur.execute('insert into OfficialSup (Region, Link, Retrieved) values (?, ?, 1)', (reg, link))
sqlite3.connect(name).commit()
FindSupporters()
大概在officialsup_table方法的錯誤。儘管如此,我的嘗試並沒有帶來好的結果。
非常感謝!
問候, 阿諾德A.
同樣爲'接近()'。 –
@alecxe非常感謝您的回答。試過,它工作得很好!還有一個問題,爲什麼你需要兩次提交()而不是一次(比如for循環之後)? – arnold
@arnold你也可以在一次提交中完成它。在進一步研究之前,我決定穩定桌子的製作。謝謝。 – alecxe