python抓取天气接口

技术栈

  1. request
  2. beautifulsoup
  3. flask

思路

  1. 选取爬取的天气网址
  2. 分析获取天气的接口及接口返回信息
  3. 解析接口返回内容
  4. 解析内容并编写自己接口实时返回天气

难易程度 ★★☆☆☆

代码

# encoding=utf-8
import json
import re
import time

import requests
from bs4 import BeautifulSoup
from flask import Flask, request

app = Flask(__name__)


@app.route('/weather/get', methods=['POST'])
def show_user_profile():
    try:
        data = request.get_data()
        dict1 = json.loads(data)
        response = {'code': 200,
                    'data': json.loads(get_weather((dict1['weather'])))}
        return json.dumps(response, ensure_ascii=False)
    except:
        return json.dumps({'code': 500})


def get_weather(city_name):
    mill = int(round(time.time() * 1000))

    headers = {'Accept': '*/*',
               'Accept-Encoding': 'gzip, deflate',
               'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
               'Connection': 'keep-alive',
               'Cookie': 'vjuids=-14768fb78.163f88bf07a.0.ee06aa72c6622; f_city=%E5%8C%85%E5%A4%B4%7C101080201%7C; UM_distinctid=166621631e8368-02bcbf8e9ce7fb-9393265-1fa400-166621631e9641; Hm_lvt_080dabacb001ad3dc8b9b9049b36d43b=1539706259,1539832348,1539832353,1539856676; vjlast=1528883311.1547965750.21; Wa_lvt_1=1547965750; Wa_lpvt_1=1547965761',
               'Host': 'd1.weather.com.cn',
               'Referer': 'http://www.weather.com.cn/weather1d/101010100.shtml',
               'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36}'}
    r = requests.get('http://d1.weather.com.cn/sk_2d/' + read_city(city_name) + '.html?_=' + str(mill), headers=headers)
    r.encoding = 'utf-8'
    return parse_html(r.text)


def parse_html(source):
    soup = BeautifulSoup(source, 'html5lib')
    text = soup.body.text
    return text.split('=')[1]


def read_city(city_name):
     with open('./citycode.txt', mode='r', encoding='utf-8') as r:
        for line in r.readlines():
            if city_name in line:
                return re.split('=', line)[0]


if __name__ == '__main__':
    app.run(host='localhost', port=8089)
    # print(json.loads(get_weather('南京')))


测试

  1. 下载依赖
pip install -r requestment.txt
  1. 运行main方法
    运行
  2. 接口测试
    postman

github

天气

博客地址

blog

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

varfun

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值