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