Python 开发Python爬虫作业python爬虫

python 爬取中国天气网气象信息

2017-09-04  本文已影响124人  suntio

实现效果如下图:

效果图

如果想实现其他功能改进一下代码吧。这个是用了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

希望给初学者一点帮助,因为我也是个初学者。

上一篇 下一篇

猜你喜欢

热点阅读