我已經重寫我的「死囚」程序中加入一些改進,使之與Python 3如何將數據添加到SQLite中的列?
這是兼容它的外觀現在:
import sqlite3
import re
import urllib.request
from bs4 import BeautifulSoup
import requests
import string
URL=[]
conn = sqlite3.connect('prison.sqlite')
conn.text_factory = str
cur = conn.cursor()
cur.execute("DROP TABLE IF EXISTS prison")
cur.execute("CREATE TABLE Prison (Execution text,Statement text, LastName text, Firstname text, TDCJNumber text, Age integer, Date text, Race text, County text)")
conn.commit()
url='http://www.tdcj.state.tx.us/death_row/dr_executed_offenders.html'
lines = urllib.request.urlopen(url)
prisondata = lines.read()
lines.close()
soup = BeautifulSoup(prisondata,"html.parser")
rows = soup.find_all('tr')
url2 = url[:38]
for row in rows:
td = row.find_all('td')
try:
Execution = str(td[0].get_text())
cur.execute("INSERT OR IGNORE INTO prison (Execution) VALUES(?);", (str(Execution),))
lastname= str(td[3].get_text())
cur.execute("INSERT OR IGNORE INTO prison (Execution) VALUES(?);", (str(Execution),))
firstname= str(td[4].get_text())
cur.execute("INSERT OR IGNORE INTO prison (Firstname) VALUES(?);", (str(firstname),))
tdcj= str(td[5].get_text())
cur.execute("INSERT OR IGNORE INTO prison (TDCJNumber) VALUES(?);", (str(tdcj),))
Age= str(td[6].get_text())
cur.execute("INSERT OR IGNORE INTO prison (Age) VALUES(?);", (str(Age),))
Date= str(td[7].get_text())
cur.execute("INSERT OR IGNORE INTO prison (Date) VALUES(?);", (str(Date),))
Race= str(td[8].get_text())
cur.execute("INSERT OR IGNORE INTO prison (Race) VALUES(?);", (str(Race),))
County= str(td[9].get_text())
cur.execute("INSERT OR IGNORE INTO prison (County) VALUES(?);", (str(County),))
links = row.find_all("a")
link = links[1].get("href")
LastStatementLink = url2 + link
lines2 = urllib.request.urlopen(LastStatementLink)
URL.append(LastStatementLink)
except Exception:
print ("An error has occured")
continue
for U in URL:
try:
r = requests.get(U)
r.raise_for_status()
print ("URL OK"), U
document = urllib.request.urlopen(U)
html = document.read()
soup = BeautifulSoup(html,"html.parser")
pattern = re.compile("Last Statement:")
Statement = soup.find(text=pattern).findNext('p').contents[0]
print (Statement.encode("utf-8"))
cur.execute("INSERT OR IGNORE INTO prison (Statement) VALUES(?);", (str(Statement),))
continue
except requests.exceptions.HTTPError as err:
print (err)
conn.commit()
conn.close()
我一直在嘗試了幾個小時以獲得正確的數據插入SQLite)數據庫,但我似乎無法得到正確的。例如:
for row in rows:
td = row.find_all('td')
try:
Execution = str(td[0].get_text())
cur.execute("INSERT OR IGNORE INTO prison (Execution) VALUES(?);", (str(Execution),))
該方案具有在規定的網站上找到該表中的所有執行數字在位置[0],並將其添加到該列的數據庫「執行」它爲所有這樣做數據的其他部分也是如此。
問題是,python似乎創建一個新的行,每次它必須添加一塊數據到數據庫。該程序運行,但我有4000行,而不是我的SQLite數據庫中約540。
我試過了很多東西,但我似乎無法將數據放入正確的列中,或者以正確數量的行結束。
我一直在想不同的方法:將數據添加到列表中,然後將它們添加到數據庫中。這意味着我將有8個數據列表(每個執行編號,名字,姓氏,......的一個列表)但是,如何將數據從列表添加到SQLite?
謝謝!
您是否嘗試更新滿足某些條件的表中的現有行或插入新行? – Fejs
'INSERT'總是添加新行 - 學習'SQL'。 – furas