實(shí)現(xiàn)效果如下圖:
如果想實(shí)現(xiàn)其他功能改進(jìn)一下代碼吧。這個是用了python冤议,requests庫
代碼如下:
import requests,json,re,city
cityname=raw_input('輸入你想得到的城市名字')
citycode=city.city.get(cityname)
url='http://d1.weather.com.cn/dingzhi/%s.html?_=1504530505680' % citycode
headers={
'Host':'d1.weather.com.cn',
'User-Agent':'Mozilla/5.0 (Windows NT 6.3; Win64; x64; rv:55.0) Gecko/20100101 Firefox/55.0',
'Accept':'*/*',
'Accept-Language':'zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',
'Accept-Encoding':'gzip, deflate',
'Referer':'http://www.weather.com.cn/weather1d/101020100.shtml',
'Connection':'keep-alive',
}
html= requests.get(url, headers=headers)
html.encoding='utf-8'
index=html.text.find('{')
last=html.text.find(';')
result=html.text[index:last]
result=json.loads(result)
result=result["weatherinfo"]
str_temp = ('%s\n%s\n%s ~ %s') % (result['cityname'],result['weather'],result['temp'],result['tempn'])
print str_temp
希望給初學(xué)者一點(diǎn)幫助买鸽,因為我也是個初學(xué)者萄金。