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


Python Output.save方法代码示例

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


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

示例1: get

# 需要导入模块: from output import Output [as 别名]
# 或者: from output.Output import save [as 别名]

#.........这里部分代码省略.........
        _threadsfile.close()
        _threads = filter(lambda x: x != '', _threads) # filter empty lines
    except IOError:
        print >> sys.stderr, "Error opening threads file " + sys.argv[1]
        sys.exit(1)

    deadthreads = set()
    # for every thread
    for url in _threads:
    # {{{
        try:
            # download thread page
            _activeparser = select_parser(url)
            if _activeparser:
                # {{{
                print >> sys.stderr, "Checking " + url,
                _threadfile = os.path.join("threads", _activeparser.outname)
                if not _activeparser.died:
            # {{{
                    _toDownload = _activeparser.get_posts_number()
                    if not _toDownload:
                        print >> sys.stderr, "- no posts numbers found, strange..."
                        break;
                    output_writer = None
                    if os.path.isfile(_threadfile): # if this thread was already downloaded
                        # {{{
                        # get posts number
                        output_writer = Output(_activeparser.outname[:-5], infile = _threadfile)
                        out_nums = set(output_writer.get_posts_number())
                        in_nums = set(_activeparser.get_posts_number())
                        deleted = list(out_nums - in_nums)
                        # leave only new posts in _toDownload
                        _toDownload = list(in_nums - out_nums)
                        for post in deleted: # set deleted marks
                            output_writer.mark_deleted(post)

                    else: # create new empty page from template
                        title = _activeparser.get_title()
                        output_writer = Output(_activeparser.outname[:-5], title = title[0],
                                               board = title[1])
                        output_writer.output.xpath('//p[@class="footer"]/a')[0].attrib['href'] = url
                    
                    # }}}

                    _toDownload.sort(cmp = lambda x,y: int(x) - int(y)) # make strict post order
                    postcnt = len(_toDownload) - 1
                    if postcnt == -1:
                        print >> sys.stderr, "- no updates"
                    else:
                        print >> sys.stderr, "- %d new posts" % (postcnt + 1)
                    for post in _toDownload:
                        # {{{
                        print >> sys.stderr, "Adding post #" + post + " (" + str(postcnt) + " left)"
                        newpost = _activeparser.get_post(post)
                        output_writer.add_post(newpost)
                        post_image = _activeparser.get_images(post)
                        if post_image:
                            print >> sys.stderr, "Downloading image for post..."
                            retry = 5
                            while retry:
                                try:
                                    output_writer.download_images(*post_image)
                                    retry = 0
                                except:
                                    time.sleep(3)
                                    retry -= 1
                            
                        postcnt -= 1
                    # }}}
                    
                    # save the thread
                    output_writer.save(_activeparser.outname)

                else: # thread has died
                    deadthreads.add(url)
                    if os.path.isfile(_threadfile): # leave mark that thread died
                        output_writer = Output(_activeparser.outname[:-5], infile = _threadfile)
                        output_writer.add_post({'topic': '', 'date': datetime.datetime.now().strftime("%a %d %b %Y %H:%M:%S"), 'postername': 'locmechan', 'postnumber': '******', 'text': html.fromstring(u'<p style="color: #ff0000; font-style: italic;">Тред умер.</p>')})
                        output_writer.output.xpath('//p[@class="footer"]/a')[0].attrib['href'] = '.'
                        output_writer.save(_activeparser.outname)
                    print >> sys.stderr, " - thread died."
                # }}}

            else: # no parser for such URL, skipping
                print >> sys.stderr, "Unsupported url: " + url
            # }}}
        except ThreadIsntAccessible:
            print >> sys.stderr, " - thread isn't accessible now due to server errors"

    # sync changes in threadlist, get links, remove dead, save back
    threadfile = open(sys.argv[2], 'r')
    filethreads = set(threadfile.read().split())
    threadfile.close()

    threadfile = open(sys.argv[2], 'w')
    filethreads = list(filethreads - deadthreads)
    filethreads.sort()
    threadfile.write('\n'.join(filethreads))
    threadfile.close()
    os.unlink('lock.pid')
开发者ID:Barafag,项目名称:locmechan,代码行数:104,代码来源:grab.py


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