File tree Expand file tree Collapse file tree 7 files changed +70
-42
lines changed
python_game/Finish-the-game Expand file tree Collapse file tree 7 files changed +70
-42
lines changed Original file line number Diff line number Diff line change 1+ #!/usr/bin/env python
2+
3+ # -*- coding: UTF-8 -*-
4+
5+
6+
7+ import urllib2 , httplib
8+ from BeautifulSoup import BeautifulSoup
9+
10+ def getIpAddr ():
11+ '''从http://www.ip.cn网站获取外网ip和地理位置'''
12+ url = 'http://www.ip.cn'
13+ html = urllib2 .urlopen (url ).read ()
14+ soup = BeautifulSoup (html )
15+ # 通过<div class="well">标签找到位置信息
16+
17+ find_div = soup .find ('div' ,{'class' :'well' })
18+ ip = find_div .code .text
19+ # 定位地理位置信息
20+
21+ # 1.获得中文地理位置
22+
23+ no = find_div .contents [0 ].text .find (';' )
24+ addr = find_div .contents [0 ].text [no + 1 :]
25+
26+ # 2.获取en文地理位置
27+
28+ # <p>GeoIP: Nanjing, Jiangsu, China</p>
29+
30+ addrEn = find_div .contents [1 ].text
31+ # 得到拼音字符串,然后进行分割,得到addr列表
32+
33+ addrEn = addrEn .split (':' )
34+ # addr取原列表中的最后一个元素
35+
36+ addrEn = addrEn [- 1 ]
37+ # 将包含位置信息的字符串用','再次分割,得到城市、省份
38+
39+ addrEn = addrEn .split (',' )
40+ # 去除字符串两边多余的空格
41+
42+ print '外网IP:' , ip
43+ print addr
44+ for n in range (len (addrEn )):
45+ addrEn [n ] = addrEn [n ].strip ()
46+ print addrEn [n ], # 逗号是为了print后不换行
47+
48+ print
49+ #print [ip, addr, addrEn]
50+
51+
52+ if __name__ == '__main__' :
53+ getIpAddr ()
Original file line number Diff line number Diff line change 1+ #coding:utf-8
2+ name = "浙江省杭州市"
3+ if name .find (u"省" ) != - 1 :# 包含'省'
4+ #print u'有省'
5+ name = name .split (u'省' )[1 ]
6+ if name .find (u"市" ) != - 1 :#包含‘市’
7+ #print u'有市'
8+ name = name .split (u'市' )[0 ]
9+ print (name )
10+ fileHandle = open ( 'test.txt' , 'w' )
11+ fileHandle .write ( name )
12+ fileHandle .close ()
Original file line number Diff line number Diff line change 1+ ����
Original file line number Diff line number Diff line change @@ -10,7 +10,10 @@ class weather(object):
1010 weather_uri = "http://apistore.baidu.com/microservice/weather?cityid="
1111 # 主处理逻辑
1212 def mainHandle (self ):
13- city_name = input ("输入你要查询的天气:" )
13+ #city_name = input("输入你要查询的天气:")
14+ fileHandle = open ( 'test.txt' , 'r' )
15+ city_name = fileHandle .read ()
16+ fileHandle .close ()
1417 uri = self .code_uri + urllib .parse .quote (city_name )
1518 #获取该城市天气情况的网址
1619 print ("查询中请等待" )
File renamed without changes.
Load Diff This file was deleted.
You can’t perform that action at this time.
0 commit comments