Skip to content

Commit 752bac6

Browse files
committed
new weather file
1 parent 9aadf95 commit 752bac6

File tree

7 files changed

+70
-42
lines changed

7 files changed

+70
-42
lines changed
7.91 KB
Loading

automatic_weather/get_ip.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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()

automatic_weather/test.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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()

automatic_weather/test.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
����

automatic_weather/天气查询.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff 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.

天气查询.py

Lines changed: 0 additions & 41 deletions
This file was deleted.

0 commit comments

Comments
 (0)