本文整理汇总了Python中models.Course.to_json_dict方法的典型用法代码示例。如果您正苦于以下问题:Python Course.to_json_dict方法的具体用法?Python Course.to_json_dict怎么用?Python Course.to_json_dict使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类models.Course
的用法示例。
在下文中一共展示了Course.to_json_dict方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: scrape_course
# 需要导入模块: from models import Course [as 别名]
# 或者: from models.Course import to_json_dict [as 别名]
def scrape_course(requester, output_dir, course_id, year, term):
"""
Scrapes all data for a particluar course, including ratings, comments,
and instructor ratings.
"""
# This course is weird. Let's skip it.
if course_id in (44050,):
print('There is some weird shit going on with course {}'.format(course_id))
return
c = Course(course_id=course_id, year=year, term=term)
base_url = '/course_evaluation_reports/fas/course_summary.html'
url = '{base_url}?course_id={course_id}'.format(base_url=base_url,
course_id=course_id)
soup = requester.make_request(url)
# Get course name, department, enrollment, etc.
if soup.h1 is None:
print('No data for course {}'.format(course_id))
return
title = soup.h1.text
colon_loc = title.find(':')
c.department, c.course_code = title[:colon_loc].split()
c.course_name = title[colon_loc + 2:]
stats = soup.select('#summaryStats')[0].text.split()
c.enrollment = int(stats[1])
c.evaluations = int(stats[3])
# Get course ratings
graph_reports = soup.select('.graphReport')
if not graph_reports:
print('No data for course {}'.format(course_id))
return
c.ratings = []
for graph_report in graph_reports[:-1]:
c.ratings += scrape_ratings(graph_report)
# Get reasons for why people signed up
c.reasons = scrape_reasons(graph_reports[-1])
c.instructors = scrape_instuctors(requester, course_id)
c.questions = scrape_questions(requester, course_id)
c.validate()
filename = os.path.join(output_dir, '{}.json'.format(c.course_id))
with open(filename, 'w') as f:
json.dump(c.to_json_dict(), f, indent=3)