', weather,re.S)
for x in res[2:]:
w = re.findall('>(.*&#63;)<', x,re.S)
for y in w[1:]:
if len(y.strip())<=0:
pass
else:
print y
print '--'*40
这样以来,我们就可以获取到了对应城市的天气情况了!!
完整代码:
#coding:UTF-8
import re
import requests
import sys
reload(sys)
sys.setdefaultencoding('UTF-8')
def ShowWeather(city):
res =str(city).split('" title="')
print res[1],'(白天-->夜间)'
html=requests.get("http://www.tianqihoubao.com/weather/{0}".format(res[0]))
weather=re.search('
(.*&#63;)
', html.text,re.S).group(1)
res=re.findall('
(.*&#63;)
', weather,re.S)
for x in res[2:]:
w = re.findall('>(.*&#63;)<', x,re.S)
for y in w[1:]:
if len(y.strip())<=0:
pass
else:
print y
print '--'*40
print '\n','*'*40
def ShowCity():
html=requests.get("http://www.tianqihoubao.com/weather/province.aspx&#63;id=420000")
citys= re.findall('