w

# -*- coding: utf-8 -*-
import pycurl
import re
import cStringIO
from pypinyin import lazy_pinyin import sys, locale buf = cStringIO.StringIO() GRAB_URL = 'http://www.gov.cn/test/2011-08/22/content_1930111.htm' c = pycurl.Curl()
c.setopt(c.HTTPHEADER, ['Accept-Charset: UTF-8'])
c.setopt(c.URL, GRAB_URL)
c.setopt(c.WRITEFUNCTION, buf.write)
c.perform() grab_data = buf.getvalue().decode('utf-8')
tmp_list = re.findall(r"\d{3}[^0]0{2}.*<", grab_data)
# 词尾一样
shi_dic = {}
# 词头一样
shi_dic_head = {}
for i in tmp_list:
tmp = i.split(' ')
city = tmp[1].split('<')
try:
shi_exist = city[0].index(u'市')
if shi_exist > 1:
# 深圳市
shi_tail = city[0].split(u'市')
# 深圳
shi = shi_tail[0]
shi_pinyin = lazy_pinyin(shi)
tail_pinyin = shi_pinyin[-1]
head_pinyin = shi_pinyin[0]
if shi_dic.has_key(tail_pinyin):
shi_dic[tail_pinyin].append(shi)
else:
shi_dic[tail_pinyin] = [shi] if shi_dic_head.has_key(head_pinyin):
shi_dic_head[head_pinyin].append(shi)
else:
shi_dic_head[head_pinyin] = [shi]
else:
continue
except Exception, e:
# print Exception, ":", e
continue def letsgo():
info = raw_input(u'请输入,如“深圳”:'.encode('utf-8')).decode(sys.stdin.encoding or locale.getpreferredencoding(True)) info_pinyin = lazy_pinyin(info)
info_shi_pinyin = info_pinyin[-1]
flag = 1
# if shi_dic.has_key(info_shi_pinyin):
# flag = 0
# for i in shi_dic[info_shi_pinyin]:
# print i
if shi_dic_head.has_key(info_shi_pinyin):
flag = 0
for i in shi_dic_head[info_shi_pinyin]:
print i
if flag == 1:
print "输入字符串非法"
die = raw_input(u'如继续,请输入go,如结束请输入其他')
if die == 'go':
letsgo()
else:
print '程序已经退出' letsgo()

  

joke python的更多相关文章

  1. Python写各大聊天系统的屏蔽脏话功能原理

    Python写各大聊天系统的屏蔽脏话功能原理 突然想到一个视频里面弹幕被和谐的一满屏的*号觉得很有趣,然后就想用python来试试写写看,结果还真玩出了点效果,思路是首先你得有一个脏话存放的仓库好到时 ...

  2. python和数据科学(Anaconda)

    Python拥有着极其丰富且稳定的数据科学工具环境.遗憾的是,对不了解的人来说这个环境犹如丛林一般(cue snake joke).在这篇文章中,我会一步一步指导你怎么进入这个PyData丛林. 你可 ...

  3. 笨办法学Python (exercise1-15)

    #exercise1print "Hello world!"print "Hello Again"print "I like typing this. ...

  4. 《笨办法学Python》

    习题一 第一个程序 print "Hello World!" print "Hello Evilxr" print "I like typing th ...

  5. Learn python the hard way. python test program 2016.04.27

    # this will not be printed in python ! print "I could have code like this." # and the comm ...

  6. python分布式抓取网页

    呵呵,前两节好像和python没多大关系..这节完全是贴代码, 这是我第一次写python,很多地方比较乱,主要就看看逻辑流程吧. 对于编码格式确实搞得我头大..取下来页面不知道是什么编码,所以先找c ...

  7. Python NLP入门教程

    本文简要介绍Python自然语言处理(NLP),使用Python的NLTK库.NLTK是Python的自然语言处理工具包,在NLP领域中,最常使用的一个Python库. 什么是NLP? 简单来说,自然 ...

  8. Python NLTK 自然语言处理入门与例程(转)

    转 https://blog.csdn.net/hzp666/article/details/79373720     Python NLTK 自然语言处理入门与例程 在这篇文章中,我们将基于 Pyt ...

  9. python 的类装饰器

    我们都知道python的函数有装饰器,那么类也有装饰器吗?有的,为什么没有呢,来看下代码吧 def out(args): def inner(cls): cls._args = args return ...

随机推荐

  1. Python json函数与Flask jsonify函数

    JSON数据结构 要把json与字典区分开来 dumps(字典转换成Json) loads(Json转换成字典) 参考: Python 的字典是一种数据结构,JSON 是一种数据格式. json 就是 ...

  2. linux启动内核源码分析

    内核的启动时从main.c这个文件里面的start_kernel函数开始的,这个文件在linux源码里面的init文件夹下面 下面我们来看看这个函数 这个函数很长,可以看个大概过去 asmlinkag ...

  3. 使用autofac的一些问题

    None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on ...

  4. STM32启动地址设置及从非0x800000 开始调试程序

    首先设置程序的启动地址,STM32默认的启动地址是从0x8000000开始的,现在我要设置程序向后偏移10K地址,也就是从0x8002800启动. 需要分两步完成上面操作: 一.Keil MDK设置: ...

  5. python学习第四十六天dir( )函数用法

    dir( )函数有点像目录的意思,但是他是包含由模块定义的名称的字符串的排序列表.这个列表包含模块中定义的所有模块,变量和函数的名称. 列举其用法 import time content = dir( ...

  6. 3486 ( Interviewe )RMQ

    Problem Description YaoYao has a company and he wants to employ m people recently. Since his company ...

  7. Vue/React如何优雅的一劳永逸的注册路由及组件

    原文链接: 本人掘金文章  假如图片看不清晰可前往掘金原文预览 官方文档: 组建注册 路由注册 未优化版: 在Vue官方文档 中,我们通过  Vue.component('MyComponentNam ...

  8. 在a标签中使用了onclick修改样式之后a:hover失效

    是因为优先级的原因造成,使用!important修改优先级. 如修改成: .button1:hover {            color: #FFF !important;            ...

  9. ubuntu16.04安装mysql数据库

    安装 sudo apt-get install mysql-server(安装过程中按提示设置root密码) sudo apt-get install mysql-client sudo apt-ge ...

  10. 转发一个robotframework的循环

    Click_Element Xpath=//b[text()='系统投放管理'] Sleep 1 Click_Element Xpath=//span[text()='全部投放情况查询'] Sleep ...