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. 001/Docker入门(Mooc)

    docker官网:https://www.docker.com/ 1.什么是docker 2.Docker思想     ==> [1].集装箱:保证程序完整(不缺东西,如配置文件等). [2]. ...

  2. github创建仓库,往github上上传自己的项目

    k第一步: 在github上创建仓库 第二步: 创建一个新的项目,填写项目名称,描述 创建完成之后,跳转到下面的页面,下面红框中的网址要记住,在后面上传代码的时候需要使用 这个地址必须要记住!!! 第 ...

  3. log4net 配置文件配置方法

    转自:http://www.dozer.cc/2013/06/log4net-config-file-order/ 最近把项目中所有的日志都改成了 log4net ,同事也蠢蠢欲动,用起了 log4n ...

  4. docker--docker介绍

    2 docker 介绍 2.1 容器技术 在计算机的世界中,容器拥有一段漫长且传奇的历史.容器与管理程序虚拟化 (hypervisor virtualization,HV)有所不同,管理程序虚拟化通过 ...

  5. HDU-4081.Qinshihuang'sNationalRoadSystem(次小生成树变种)

    Qin Shi Huang's National Road System Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/3 ...

  6. 《剑指offer》面试题20 顺时针打印矩阵 Java版

    我的方法:遇到这种题最好在纸上画一画打印路线.我利用了4个标志left.top.right.bottom,表示当前需要打印的左界.上届.右界和下界,换句话说这些界线之外的已经打印了,如此一来判断结束的 ...

  7. Django之模板(T)

    一, 常用语法 Django模板中只需要记两种特殊符号: {{ }}和 {% %} {{ }}表示变量,在模板渲染的时候替换成值,{% %}表示逻辑相关的操作. 一, 变量 语法: {{ 变量名 }} ...

  8. 给定两个list A ,B,请用找出 A ,B中相同的元素,A ,B中不同的元素 ??

    A.B 中相同元素:print(set(A)&set(B)) A.B 中不同元素:print(set(A)^set(B))

  9. 基于 Python 的自定义分页组件

    基于 Python 的自定义分页组件 分页是网页中经常用到的地方,所以将分页功能分出来,作为一个组件可以方便地使用. 分页实际上就是不同的 url ,通过这些 url 获取不同的数据. 业务逻辑简介 ...

  10. 解决 myEclipse与tomcat 不同步的问题

    在我们使用eclipse做web调试的过程中,一般只需要在eclipse修改程序,然后在浏览器刷新就能发现文件更改,今天突然发现保存后不能更改了.1.检查tomcat中的文件发现文件没有更新.2.检查 ...