Python之钉钉机器人推送天气预报

通过Python脚本结合钉钉机器人,定时向钉钉群推送天气预报


#!/usr/bin/python
# -*- coding: utf-8 -*-
# Author: aiker@gdedu.ml
# My blog http://m51cto.51cto.blog.com 
import requests
import re
import urllib2
import json
import sys
import osheaders = {'Content-Type': 'application/json;charset=utf-8'}
api_url = "https://oapi.dingtalk.com/robot/send?access_token=37e23308d1b84eb4ac34566e03c4c4e74bxxx7xxxxxxxxxxx"
##从钉钉机器人设置中拷贝  
def msg(text):json_text= {"msgtype": "text","at": {"atMobiles": ["132xxxx1280"],"isAtAll": False},"text": {"content": text}}print requests.post(api_url,json.dumps(json_text),headers=headers).contenthearders = "User-Agent","Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36"url = "https://tianqi.moji.com/weather/china/guangdong/shenzhen"    ##要爬去天气预报的网址
par = '(<meta name="description" content=")(.*?)(">)'    ##正则匹配,匹配出网页内要的内容##创建opener对象并设置为全局对象
opener = urllib2.build_opener()
opener.addheaders = [hearders]
urllib2.install_opener(opener)##获取网页
html = urllib2.urlopen(url).read().decode("utf-8")##提取需要爬取的内容
data = re.search(par,html).group(2)
msg(data)

拷贝脚本时请去掉注释
运行脚本:

python weather.py 
{"errcode":0,"errmsg":"ok"}

钉钉群收到消息如图:

Python之钉钉机器人推送天气预报

做计划任务:

# crontab -e
no crontab for root - using an empty one
30 8 * * * /usr/bin/python /root/script/weather.py 2> /dev/null > /dev/null

每天早晨8:30自动推送天气预报到钉钉群



本文转自 喵来个鱼 51CTO博客,原文链接:http://blog.51cto.com/m51cto/2072280,如需转载请自行联系原作者

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/279122.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

google +按钮_如何禁用或改善Google的Google+集成

google 按钮If you’ve used Google lately, you’ve probably seen Google taking over Google’s search results. You don’t have to put up with it — you can disable the integration, show better social-networking pages or hide those pesky Google notifications.…

java 集合读写同步_JAVA多线程学习十六 - 同步集合类的应用

1.引言在多线程的环境中&#xff0c;如果想要使用容器类&#xff0c;就需要注意所使用的容器类是否是线程安全的。在最早开始&#xff0c;人们一般都在使用同步容器(Vector,HashTable),其基本的原理&#xff0c;就是针对容器的每一个操作&#xff0c;都添加synchronized来进行同…

ubuntu自定义菜单_如何自定义Ubuntu的每日消息

ubuntu自定义菜单Ubuntu displays an informative message, known as the message of the day, when a user logs in at the terminal. The MOTD is fully customizable — you can add your own text and other dynamic data. 当用户在终端上登录时&#xff0c;Ubuntu将显示信…

火狐web开发清楚缓存_如何使用Firefox的Web开发工具

火狐web开发清楚缓存Firefox’s Web Developer menu contains tools for inspecting pages, executing arbitrary JavaScript code, and viewing HTTP requests and other messages. Firefox 10 added an all-new Inspector tool and updated Scratchpad. Firefox的Web Develop…

Java基类共同属性设置_多选择基类的访问属性-Java初学笔记

多选择基类的访问属性你现在知道在定义类的访间属性时可用的选择项&#xff0c;你希望使用这些类定义子类。你知道在类继承上这些属性所具有的效果&#xff0c;但是你如何决定到底应该使用哪一个呢?这里没有死板和现成的规则&#xff0c;你选择的访问属性取决于在将来你想用类…

IT:如何在Windows Server 2008 R2上安装Hyper-V虚拟化

Windows Server 2008 R2 and later releases of the product ship with a virtualization platform called Hyper-V, which works quite well since it’s built into Windows. Today we’re going to show you how to install it. Windows Server 2008 R2和更高版本的产品附带…

FineReport单行与数据库交互的方法

1. 问题描述 我们在做一张报表填报的时候经常会遇到需要在一行进行添加动作&#xff0c;将该行数据直接与数据库交互&#xff0c;执行存储过程过程。我们可以通过每一行增加帆软“插入”按钮实现插入动作&#xff0c;并且在控件事件中增加和数据库的交互&#xff0c;但当事件…

java cas volatile_每日一个知识点:Volatile 和 CAS 的弊端之总线风暴

每日一个知识点系列的目的是针对某一个知识点进行概括性总结&#xff0c;可在一分钟内完成知识点的阅读理解&#xff0c;此处不涉及详细的原理性解读。一、什么是总线风暴总线风暴&#xff0c;听着真是一个帅气的词语&#xff0c;但如果发生在你的系统上那就不是很美丽了&#…

SqlServer之代码块相关

转载必需注明出处:http://www.ncloud.hk/%E6%8A%80%E6%9C%AF%E5%88%86%E4%BA%AB/sqlserver-codeblock/ 一、go语句 Go语句是SqlServer中用来表示当前代码块结束提交并确认结果的语句。 Go语句不能和其他Sql命令卸载同一行上&#xff01; 定义的局部变量作用域局限在定义它的代码…

010 使用list和tuple

list Python内置的一种数据类型是列表&#xff1a;list。list是一种有序的集合&#xff0c;可以随时添加和删除其中的元素。 比如&#xff0c;列出班里所有同学的名字&#xff0c;就可以用一个list表示&#xff1a; >>> classmates [Michael, Bob, Tracy] >>&g…

IT:如何使用Server 2008 R2上的远程桌面服务设置自己的终端服务器

In today’s IT learning article, we are going to take a look at installing Terminal Services, otherwise known as Remote Desktop Services, on a Server 2008 R2 machine. 在今天的IT学习文章中&#xff0c;我们将介绍在Server 2008 R2计算机上安装终端服务(也称为远程…

Win10 jdk的安装以及环境变量的配置,及需要注意的坑

此篇文章献给自己&#xff0c;希望下次长点记性 最近本人终于有时间开始学习appium&#xff0c;并且开始在电脑上配置环境&#xff0c;第一步就是在我那刚装的Win10 系统上安装jdk&#xff0c;过程并不顺利&#xff0c;由于之前都是用的win7&#xff0c;几乎都是一路的下一步&a…

Jenkins配置Findbugs做源代码安全扫描

2019独角兽企业重金招聘Python工程师标准>>> 此内容目标阅读用户&#xff1a;运维人员 配置步骤如下&#xff1a; Jenkins安装Findbugs插件 Jenkins系统管理 → 管理插件 → (可选插件)找到Findbugs及其依赖插件全部安装成功&#xff0c;Jenkins重启&#xff0c;即可…

如何从USB运行Windows 8 Developer Preview

Running Windows 8 from a USB should not be confused with installing Windows on a USB drive–in this case, instead of installing it on the drive, we’re just running it straight from the portable drive. Here’s how to do it. 从USB运行Windows 8不应与在USB驱动…

火狐查cookie_Firefox 65默认会阻止跟踪Cookie

火狐查cookieMozilla today released Firefox 63, which includes an experimental option to block third-party tracking cookies, protecting against cross-site tracking. You can test this out today, but Mozilla wants to enable it for everyone by default in Firef…

chromebook刷机_如何将iTunes音乐移至Chromebook

chromebook刷机If you switch between platforms a lot, you know it’s a hassle to move your stuff around. Fortunately, music files don’t have any sort of DRM tying them to a specific platform the way that movies do, so you can copy and paste your library ar…

阿里巴巴Java开发手册终极版

2019独角兽企业重金招聘Python工程师标准>>> 一、编程规约&#xff1a; (一)命名风格 1. 【强制】 代码中的命名均不能以下划线或美元符号开始&#xff0c;也不能以下划线或美元符号结束。 反例&#xff1a; _name / __name / $Object / name_ / name$ / Object$ 2.…

ios6.1.6可用微信_这是iOS 12.1的新增功能,今天可用

ios6.1.6可用微信While iOS 12 is still fairly fresh, the first point release will be rolling out starting today. This brings a handful of new features, like Group Facetime, dual SIM support, camera improvements, new emoji, and more. 尽管iOS 12仍然相当新鲜&a…

esp32 cam工作电流_我如何在家工作:Cam的生产力之痛

esp32 cam工作电流Telecommuting is becoming more and more common these days, with many tech writers (myself included) working from home on a full-time basis. I get asked about how I work fairly often, so here’s the skinny. 如今&#xff0c;远程办公变得越来越…

NUMPY数据集练习 ----------SKLEARN类

123456<br><br># 1. 安装scipy&#xff0c;numpy&#xff0c;sklearn包import numpyfrom sklearn.datasets import load_iris# 2. 从sklearn包自带的数据集中读出鸢尾花数据集dataprint(data.data)123# 3.查看data类型&#xff0c;包含哪些数据data load_iris()pr…