我真的花了最宝贵的时间试图找出我收到的错误消息的原因。
我正在编写一个web scraper,它使用python和beautifulsoup进行scraping,peewee进行数据库交互,将procycing stats中的数据刮到mysql数据库中。webscraper工作得非常好,但是我在将数据插入mysql表时遇到了一些问题。
首先,我使用peewee的 create_tables()
功能。在下面,我粘贴了peewee模型的代码,它包含在我调用的一个文件中 peewee_lib.py
.
from peewee import *
from mysql_login_info import *
results_database = MySQLDatabase(mysql_db_name, user=mysql_uname, password=mysql_pw, host='localhost')
class BaseModel(Model):
class Meta:
database = results_database
class Rider(BaseModel):
pcsid = IntegerField()
name = CharField()
class Race(BaseModel):
name = CharField()
class Result(BaseModel):
name = CharField()
year = IntegerField()
date = DateField()
position = IntegerField()
points_pcs = IntegerField()
race = ForeignKeyField(Race, backref='results')
rider = ForeignKeyField(Rider, backref='results')
接下来,我使用一个文件 scrape_to_peewee.py
创建将我的类定义从我的刮库“绑定”在一起的类 scraper_lib.py
还有前面提到的peewee图书馆, peewee_lib.py
.
这是我的密码 scrape_to_peewee.py
:
import scraper_lib as pylib
import peewee_lib as pw
class Sheet_bind:
def __init__(self, rider_obj, sheet):
self.year = sheet.year
self.rider = sheet.rider
self.rows = []
for row in sheet.rows:
if row.row_type == "tour_header":
pass
else:
temp_query = pw.Race.select().where(pw.Race.name == row.race)
if not temp_query.exists():
temp_query = pw.Race(name=row.race)
temp_query.save()
else:
pass
temp_res = pw.Result(name=row.name,\
year=sheet.year,\
position=row.result,\
points_pcs=row.points_pcs)
if row.row_type in ["stage", "classification"]:
temp_res.name = row.race + ' ' + row.name
temp_res.race=temp_query
temp_res.rider=rider_obj
temp_res.save()
temp_query = None
temp_res = None
class Rider_bind:
def __init__(self, rider_id):
self.rider_py = pylib.Rider(rider_id)
self.rider_pw = pw.Rider(pcsid=self.rider_py.url_id, name=self.rider_py.name)
self.rider_pw.save()
def load_sheets(self, start_year, end_year):
for year in xrange(start_year, end_year + 1):
if year not in self.rider_py.sheets:
self.rider_py.load_sheets(year, year)
loaded_sheet = Sheet_bind(self.rider_pw, self.rider_py.sheets[year])
loaded_sheet.save()
def main():
pw.results_database.connect()
main()
在将这个最终文件加载到解释器之后,我尝试将一个示例附加程序加载到数据库中。启动 Rider_bind
上课很顺利,我仔细检查了一遍,确保有一行已经写给了我的老师 rider
mysql中的表。当我尝试将结果加载到数据库时 Rider_bind.load_sheets()
但是,我得到以下错误:
$ python
Python 2.7.15rc1 (default, Nov 12 2018, 14:31:15)
[GCC 7.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from scrape_to_peewee import *
>>> olly = Rider_bind("oliver-naesen")
>>> olly.load_sheets(2018, 2018)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "scrape_to_peewee.py", line 55, in load_sheets
loaded_sheet = Sheet_bind(self.rider_pw, self.rider_py.sheets[year])
File "scrape_to_peewee.py", line 33, in __init__
temp_res.race=temp_query
File "/home/trenza/.local/lib/python2.7/site-packages/peewee.py", line 3848, in __set__
if obj != fk_value and self.name in instance.__rel__:
File "/home/trenza/.local/lib/python2.7/site-packages/peewee.py", line 726, in __ne__
return not (self == other)
File "/home/trenza/.local/lib/python2.7/site-packages/peewee.py", line 723, in __eq__
return self._hash == other._hash
AttributeError: 'NoneType' object has no attribute '_hash'
问题似乎与将一个peewee模型分配给foreignkey字段有关。当我把通话顺序颠倒过来 temp_res.rider = rider_obj
首先,它给了我同样的错误,回溯指向那个调用。
从peewee文档来看,foreignkey字段应该像将另一个peewee类作为值赋给它们一样简单。有人知道我错在哪里吗?任何帮助都将不胜感激。
谢谢您!
编辑:
不是这个问题的重复,因为它(据我所知)与 select
呼叫(上述问题中的问题)。
1条答案
按热度按时间cs7cruho1#
在指定属性时,需要将“temp\u query”解析为对象。