---
title: "python写vim script 之 糗事百科"
author: "shrek"
date: "2009-02-24T12:08:16Z"
tags: ["Python", "XML", "vim", "工作", "脚本"]
slug: "python-vim-script"
draft: false
lastmod: "2009-02-24T12:08:16Z"
id: 337862
url: "https://shrek.iteye.com/blog/337862"
---

工作累了，难免想开点小差，弄点轻松的东西看看，又不能让老大发现
网上找了个 取糗事百科的 python 脚本，稍改了下，改成vim script，
这样编辑文件的时候，随便开个小差，在vim里就可以看了....



```java

function! QiouShi()
python << EOF
import urllib
import urllib2
import vim
from xml.sax.saxutils import unescape
from BeautifulSoup import BeautifulSoup          # For processing HTML

def formalize(text):
    result = ''
    lines = text.split(u'\n')
    for line in lines:
        line = line.strip()
        if len(line) == 0:
            continue
        result += line + u'\n\n'
    return result

def grab():
  count = 0
  page=1
  url = "http://qiushibaike.com/qiushi/best/all/page/%d" % page
  data = urllib2.urlopen(url).readlines()
  soup = BeautifulSoup("".join(data))
  contents = soup.findAll('div', "content")
  stories = [str(text) for text in contents]
  for story in stories:
      count += 1
      minisoup = BeautifulSoup(story)
      text = ''.join([e for e in minisoup.recursivechildgenerator() if isinstance(e, unicode)])
      text = urllib.unquote(unescape(text, {'&quot;':'"'}))
      text = formalize(text).encode("gbk")
      vim.current.buffer.append( '-' * 20 + " %05d " % count + '-' * 20  )
      for line in text.split("\n"):
        vim.current.buffer.append( line )
      vim.current.buffer.append("")

vim.current.buffer[:]=None
grab()
EOF
endfunction

command! -nargs=0 SzQiouShi :call QiouShi()


```