def get_senate_ratings():
url = get_latest_url('http://www.cookpolitical.com/node/4060')
doc = parse(url).getroot()
good_tds = []
for td in doc.cssselect('td'):
d = dict(td.items())
if not d.has_key('width') or not d['width'] == '92':
continue
data = [x for x in list(td.itertext()) if x.strip()]
if len(data) == 1:
continue
rating = re.sub(r' \(.*$', '', data[0]) \
.lower() \
.replace(' ', '_') \
.replace('toss_up', 'tossup') \
data = data[1:]
for race in data:
state = re.search(r'[A-Z]{2}', race).group()
district = ''
body = 'S'
cr, created = CookRating.objects.get_or_create(body=body,
state=state,
district=district,
rating=rating)
cr.save()
评论列表
文章目录