当前位置: 首页>>代码示例>>Python>>正文


Python Movie.set_name方法代码示例

本文整理汇总了Python中movie.Movie.set_name方法的典型用法代码示例。如果您正苦于以下问题:Python Movie.set_name方法的具体用法?Python Movie.set_name怎么用?Python Movie.set_name使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在movie.Movie的用法示例。


在下文中一共展示了Movie.set_name方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: DoubanHtmlParser

# 需要导入模块: from movie import Movie [as 别名]
# 或者: from movie.Movie import set_name [as 别名]

#.........这里部分代码省略.........

    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

    # Invoked when endtag is parsed.
    def handle_endtag(self, tag):
        if tag == self._output_entry_finish_tag:
            # This is a movie entry end tag. Finishi this movie object.
            self._movie_list.append(self._movie)
            self.clear()
        elif tag == self._output_finish_tag:
            if self._start_movie_name:
                # This is a movie name end tag.
                self._movie.set_name(self._current_data)
                self._start_movie_name = False
                self._current_data = ''
            if self._start_intro:
                # this is a movie intro end tag.
                self._movie.set_intro(self._current_data)
                self._start_intro = False
                self._current_data = ''
            if self._start_date:
                # this is a movie date end tag.
                self._movie.set_date(self._current_data)
                self._start_date = False
                self._current_data = ''
            # Stop storing data.
            self._grab_data = False
    
    # Invoked when a data if parsed.
    def handle_data(self, data):
        if self._grab_data:
            # Upon storing data, append all the data to _current_data.
            self._current_data += data

    # return the list of movies parsed.
    def get_movie_list(self):
        return self._movie_list
开发者ID:halfprice,项目名称:douban-movie,代码行数:104,代码来源:html_parser.py


注:本文中的movie.Movie.set_name方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。