python写vim script 之 糗事百科

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


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, {'"':'"'}))
      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()

你可能感兴趣的:(vim,Python,脚本,XML,工作)