Python Simple crawl oil price information sent to the public number

Source: Internet
Author: User
Tags python script

Based on this period of time, Guangzhou's oil price change is relatively large, so I myself view the price of the site also increased, but as aProgrammerIsn't that an insult?? Can use the code to achieve why use manpower??? So I made a simple Python script to climb the oil price information, and later found that the execution of scripts or more trouble, on a regular basis to crawl the information sent to the corresponding public number! Words not much to say, directly on the script.

(ENV1)  ?  python_scripts cat youjia_wechat.py#!/usr/bin/python#-*- coding:utf-8  -*-#__author__  ==  ' chenmingle ' import requestsimport jsonimport sysreload (SYS) Sys.setdefaultencoding (' Utf-8 ') import urllib3import timeimport  urllibimport reurl  = r ' http://youjia.chemcp.com/guangdong/guangzhoushi.html ' res = urllib.urlopen (URL) HTML  = res.read (). Decode (' gb2312 ') par =  ' (&nbsp;&nbsp;&nbsp;&nbsp;) (. *?) (<br />) ' Web = re.search (par,html). Group (2) web = web.split ('    ', 1) [0] Cml = web.replace (' <font color= ' Red > ', ': ') cml = cml.replace (' </font> ', ' ' Cml = cml.replace (', ', ' \ n ') cml = cml.replace (': ', ' \ n ', 1) cml = cml.replace (', ', ') \ n ') print cmlclass wechat:    def __init__ (Self,corpid,secret):         url =  ' https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=%s&corpsecret=%s '  %   (Corpid,secret)         res = self.url_req (URL)          self.token = res["Access_token"]     Def url_req (self,url):         requests.packages.urllib3.disable_ Warnings ()         req = requests.get (Url, verify=False)         res = json.loads (Req.text)          return res    def send_message (self,user,content):         url =  "Https://qyapi.weixin.qq.com/cgi-bin/message/send? access_token=%s " % self.token        data = {                  "Touser": user,                  "Msgtype":  "text",                  "Agentid":  1000002,                 "Text" : {                          "Content": content                         },                  "safe": "0"                  }        &nbSp;requests.packages.urllib3.disable_warnings ()         res =  requests.post (Url,json=data, verify=false)         if  Json.loads (res.content) [' errmsg '] ==  ' OK ':             return  "send message sucessed"          else:            return reslocaltime  = time.strftime ("%y-%m-%d %h:%m:%s") a =  "Guangzhou Oil Price Information:" if __name__ ==  "__ main__ ':    user =  ' @all '     title = a     msg = cml    content = a +  ' \ n '  + cml     get_token = wechat (' wxf****** ', ' **********pfou7***8 ')      print get_token.send_messAge (User, content) 


Finally add crontab on the timed execution script:

(ENV1)? Python_scripts CRONTAB-L45 8 * * */usr/local/bin/python/home/python_scripts/youjia_wechat.py >/dev/null 2>& ; 1


On the public number, you'll know the daily oil price.


Python beginners, so look at the great gods and many advice

Python Simple crawl oil price information sent to the public number

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.