Python's spike robot push weather forecast

Source: Internet
Author: User
Tags python script

Send the weather forecast to the nail group by the Python script combined with the nail robot

#!/usr/bin/python#-*-coding:utf-8-*-# Author: [email protected]# My blog http://m51cto.51cto.blog.com Import req Uestsimport reimport urllib2import jsonimport sysimport 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=") (. *?) (">) ' # #正则匹配, match what you want in the page # #创建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)

拷贝脚本时请去掉注释
To run the script:

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

Nail Group received Message

To do a planning task:

# crontab -eno crontab for root - using an empty one30 8 * * * /usr/bin/python /root/script/weather.py 2> /dev/null > /dev/null

Auto-push weather forecasts to nail groups 8:30 daily

Python's spike robot push weather forecast

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.