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()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值