接口地址
天气地址: 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"}