Python抓取7日内天气并发送飞书机器人消息

接口地址

天气地址: http://www.weather.com.cn/weather/101010100.shtml
飞书api: https://open.feishu.cn/open-apis/bot/v2/hook/0d28b43b-d744-4980-8ec9-**********

代码

import json

import requests as req
from bs4 import BeautifulSoup

city_names = ['北京', '濮阳']
city_codes = ['101010100', '101181301']

content = ''
for i, city_code in enumerate(city_codes):
    content += '**' + city_names[i] + '** \n'

    data = req.get(url='http://www.weather.com.cn/weather/' + city_code + '.shtml')
    data.encoding = 'utf-8'
    html = data.text
    soup = BeautifulSoup(html, 'html.parser').find('ul', 't clearfix')
    day_list = soup.find_all('h1')
    wea = soup.find_all('p', 'wea')
    tem1 = soup.select('p.tem > span')
    tem2 = soup.select('p.tem > i')

    for j in range(0, 6):
        content += day_list[j].text + ':' + '    ' + wea[j].text + '    ' + (
            '' if tem1[j] is None else tem1[j].text + '/') + tem2[j].text + ' \n'
    if i < len(city_codes) - 1:
        content += ' --- \n '

fs_data = {'msg_type': 'interactive',
           'card': {'header': {'title': {'tag': 'plain_text', 'content': '7日内天气'}, 'template': 'blue'},
                    'elements': [{'tag': 'markdown', 'content': content}]}}
print(json.dumps(fs_data, ensure_ascii=False))
# 修改 header
headers = {"Content-Type": "application/json;charset=utf-8"}
result = req.post(url='https://open.feishu.cn/open-apis/bot/v2/hook/0d28b43b-d744-4980-8ec9-**********',
                  json=fs_data, headers=headers)
result.encoding = 'utf-8'
print(result.text)

运行结果

{"msg_type": "interactive", "card": {"header": {"title": {"tag": "plain_text", "content": "7日内天气"}, "template": "blue"}, "elements": [{"tag": "markdown", "content": "**北京** \n29日(今天):    多云    27/18℃ \n30日(明天):    多云转晴    29/18℃ \n31日(后天):    晴    31/17℃ \n1日(周四):    晴转多云    29/18℃ \n2日(周五):    多云    30/18℃ \n3日(周六):    多云    29/21℃ \n --- \n **濮阳** \n29日(今天):    阵雨转小雨    20/15℃ \n30日(明天):    小雨转多云    25/17℃ \n31日(后天):    晴转多云    28/19℃ \n1日(周四):    多云    30/17℃ \n2日(周五):    多云    29/16℃ \n3日(周六):    阴    27/18℃ \n"}]}}
{"StatusCode":0,"StatusMessage":"success"}
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值