【发布时间】:2015-01-07 22:55:42
【问题描述】:
好的,我遇到的主要问题是我有一个不应该有任何重复条目的表。这是因为我希望有一个将在单独的表中引用的主键。
根据我对规范化的了解,最好以这种方式设计您的数据库。所以现在我有一个包含一堆重复条目的表(应该只有 6 个唯一条目,但有 30 个条目,每个唯一条目重复 5 次)。
我应该如何解决这个问题?我应该在导入数据时解决这个问题,还是使用 UNIQUE 关键字。请注意,当我这次尝试使用 UNIQUE 功能时,它给了我一个错误,因为我正在导入的数据确实有重复条目。
编辑:
这是我的物品的样子:
from scrapy.item import Item, Field
class TeamStats(Item):
# define the fields for your item here like:
# name = scrapy.Field()
team = Field()
division = Field()
rosterurl = Field()
player_desc = Field()
playerurl = Field()
pass
class Player(Item):
exp = Field()
pass
这是我的代码的样子:
import scrapy
import string
import re
from scrapy.selector import HtmlXPathSelector ##needed to import xpath command
from scrapy.shell import inspect_response ##needed for Response object
from nbastats.items import TeamStats, Player ##needed to import player stats
class NbastatsSpider(scrapy.Spider):
name = "nbaStats"
start_urls = [
"http://espn.go.com/nba/teams" ##only start not allowed because had some issues when navigated to team roster pages
]
def parse(self,response):
items = [] ##array or list that stores TeamStats item
i=0 ##counter needed for older code
for division in response.xpath('//div[@id="content"]//div[contains(@class, "mod-teams-list-medium")]'):
for team in division.xpath('.//div[contains(@class, "mod-content")]//li'):
item = TeamStats()
item['division'] = division.xpath('.//div[contains(@class, "mod-header")]/h4/text()').extract()[0]
item['team'] = team.xpath('.//h5/a/text()').extract()[0]
item['rosterurl'] = "http://espn.go.com" + team.xpath('.//div/span[2]/a[3]/@href').extract()[0]
items.append(item)
print(item['rosterurl'])
request = scrapy.Request(item['rosterurl'], callback = self.parseWPNow)
request.meta['play'] = item
yield request
def parseWPNow(self, response):
item = response.meta['play']
item = self.parseRoster(item, response)
return item
def parseRoster(self, item, response):
players1 = []
int = 0
for players in response.xpath("//td[@class='sortcell']"):
play = {}
play['name'] = players.xpath("a/text()").extract()[0]
play['position'] = players.xpath("following-sibling::td[1]").extract()[0]
play['age'] = players.xpath("following-sibling::td[2]").extract()[0]
play['height'] = players.xpath("following-sibling::td[3]").extract()[0]
play['weight'] = players.xpath("following-sibling::td[4]").extract()[0]
play['college'] = players.xpath("following-sibling::td[5]").extract()[0]
play['salary'] = players.xpath("following-sibling::td[6]").extract()[0]
players1.append(play)
item['playerurl'] = response.xpath("//td[@class='sortcell']/a").extract()
item['player_desc']=players1
return item
这是我的管道的样子:
class NbastatsPipeline(object):
def __init__(self):
self.setupDBCon()
self.createTables()
def setupDBCon(self):
self.con = lite.connect('test.db')
self.cur = self.con.cursor()
def createTables(self):
self.dropTeamsTable()
self.dropPlayersTable()
self.dropDivsTable()
self.createTeamsTable()
self.createPlayersTable()
self.createDivsTable()
def createTeamsTable(self):
self.cur.execute("CREATE TABLE IF NOT EXISTS Teams(P_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, \
team TEXT, \
DivId INTEGER, \
FOREIGN KEY (DivId) REFERENCES Divs1(Did) \
)")
def createDivsTable(self):
self.cur.execute("CREATE TABLE IF NOT EXISTS Divs(Did INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, \
division TEXT)")
def createPlayersTable(self):
self.cur.execute("CREATE TABLE IF NOT EXISTS Players(player_name TEXT, \
salary TEXT, \
weight INTEGER, \
age INTEGER, \
college TEXT )")
def dropTeamsTable(self):
self.cur.execute("DROP TABLE IF EXISTS Teams")
def dropPlayersTable(self):
self.cur.execute("DROP TABLE IF EXISTS Players")
def dropDivsTable(self):
self.cur.execute("DROP TABLE IF EXISTS Divs")
def closeDB(self):
self.con.close()
def __del__(self):
self.closeDB()
def process_item(self, item, spider):
for key, value in item.iteritems():
if key == "division":
print(item.get('division', ""))
self.cur.execute("INSERT INTO Divs( division ) VALUES(?)", (item.get('division', ""),))
self.con.commit()
# self.storeInDb(item) #this is the line you'll use when ready to completely pass item through to storeInDb but it'll be lower in code
return item
【问题讨论】:
标签: python database sqlite web-scraping scrapy