當前位置: 首頁>>代碼示例>>Python>>正文


Python Course.to_json_dict方法代碼示例

本文整理匯總了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)
開發者ID:jpalay,項目名稱:qscrapy,代碼行數:53,代碼來源:scrapers.py


注:本文中的models.Course.to_json_dict方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。