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


Python Movie.set_rating方法代碼示例

本文整理匯總了Python中movie.Movie.set_rating方法的典型用法代碼示例。如果您正苦於以下問題:Python Movie.set_rating方法的具體用法?Python Movie.set_rating怎麽用?Python Movie.set_rating使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在movie.Movie的用法示例。


在下文中一共展示了Movie.set_rating方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: DoubanHtmlParser

# 需要導入模塊: from movie import Movie [as 別名]
# 或者: from movie.Movie import set_rating [as 別名]
class DoubanHtmlParser(HTMLParser):

    def __init__(self):
        HTMLParser.__init__(self)
        # If True, the content of this tag is a movie entry.
        self._output_entry = False
        # If this tag is closed, finish this movie entry.
        self._output_entry_finish_tag = ''

        # If True, the content of this tag is movie name.
        self._start_movie_name = False
        # If True, the content of this tag is movie intro.
        self._start_intro = False
        # If True, the content of this tag is movie viewing date.
        self._start_date = False

        # If True, store next content in _current_data
        self._grab_data = False
        # If this tag is encountered, finish current entry/attributes
        # and update current movie object.
        self._output_finish_tag = ''

        # A list of movie object
        self._movie_list = []
        # Current discovered movie object
        self._movie = None

        # a buffer storing data.
        self._current_data = ''

    def reset(self):
        HTMLParser.reset(self)
        # Reser clears movie list
        self._movie_list = []

    def clear(self):
        # clear all the states of current movie.
        # Prepare for next movie entry.
        self._output_entry = False
        self._output_entry_finish_tag = ''

        self._start_movie_name = False
        self._start_intro = False
        self._start_date = False

        self._grab_data = False
        self._output_finish_tag = ''

        self._current_data = ''
        self._movie = None

    # Invoked when a html start tag is parsed.
    def handle_starttag(self, tag, attrs):
        if tag == 'div':
            for attr in attrs:
                if attr[0] == 'class' and attr[1] == 'info':
                    # <div class="info"> starts a new movie entry.
                    self._output_entry = True
                    self._movie = Movie()
                    self._output_entry_finish_tag = tag
        elif self._output_entry:
            if tag == 'li':
                for attr in attrs:
                    if attr[0] == 'class' and attr[1] == 'title':
                        # <li class="title"> includes movie name
                        self._start_movie_name = True
                    elif attr[0] == 'class' and attr[1] == 'intro':
                        # <li class="intro"> includes movie intro
                        self._output_finish_tag = tag
                        self._grab_data = True
                        self._start_intro = True
            elif tag == 'span':
                for attr in attrs:
                    if attr[0] == 'class' and attr[1] == 'date':
                        # <span class="date"> includes movie view date.
                        self._output_finish_tag = tag
                        self._grab_data = True
                        self._start_date = True
                        #TODO
                        # watched list has
                        #     <dev class="info">
                        #       everything
                        #     </dev>
                        # but wish list has
                        #     <dev class="info">
                        #        movie info
                        #     </dev>
                        #     <li>
                        #        entry creation day
                        #     </li>
                    if attr[0] == 'class' and attr[1].find('rating') >= 0:
                        # <span class="ratingX-t"> has rating information.
                        def GetRating(s):
                            return ((s.split('rating'))[1].split('-'))[0]
                        self._movie.set_rating(int(GetRating(attr[1])))
            elif self._start_movie_name and tag == MOVIE_NAME_START_TAG:
                # movie name tag is encountered. Store next data.
                self._output_finish_tag = tag
                self._grab_data = True

#.........這裏部分代碼省略.........
開發者ID:halfprice,項目名稱:douban-movie,代碼行數:103,代碼來源:html_parser.py


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