python爬取中国天气网的天气

越来越多的天气开始进行收费了,今天想到用python爬取其他网站上的天气,岂不美哉

import requests as rq
import json
import re


def print_hi(name):
    # 城市Id,城市ID对照表放下面了,若是获取可根据IP地址获取地区,然后再对照下面的对照表进行动态获取
    cityId = '101110101'
    url = 'http://d1.weather.com.cn/weather_index/' + cityId + '.html'
    res = rq.get(url)
    resText = res.text.encode(res.encoding).decode(res.apparent_encoding)
    cityDZ = re.findall(r'var cityDZ =.*?;', resText)[0]
    print(cityDZ[12:-1])
    dataSK = re.findall(r'var dataSK =.*?;', resText)[0]
    print(dataSK[12:-1])
    dataZS = re.findall(r'var dataZS =.*?;', resText)[0]
    print(dataZS[12:-1])
    fc = re.findall(r'var fc =(.*)', resText)[0]
    print(fc)
    cityDZJson = json.loads(cityDZ[12:-1])
    print('城市:' + cityDZJson['weatherinfo']['city'])
    print('天气:' + cityDZJson['weatherinfo']['weather'])
    print('最高温度:' + cityDZJson['weatherinfo']['temp'])
    print('最低温低:' + cityDZJson['weatherinfo']['tempn'])
    print('风向:' + cityDZJson['weatherinfo']['wd'])
    print('风向强度:' + cityDZJson['weatherinfo']['ws'])
	# 其他的字段请自行获取

if __name__ == '__main__':
    print_hi('PyCharm')

说我字数太多,不给发布,没办法了,放到git上https://gitee.com/lk666/happy-birthday/blob/master/other/city.js

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 9
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值