【上班摸魚】聊天機器人定時發送微博熱搜


【上班摸魚】聊天機器人定時發送微博實時熱搜新聞

序言

辦公室總有一些人工作做的好,資訊掌握也不少,且總能第一時間獲取最新資訊,某房地產公司基金快爆雷了,某男星劈腿女星出軌。
圖1
作為搬磚能手的我們,也想實時關注國家大事,但又不想下載各種app以及打開某個頁面。有沒有一款功能能定時推送微博各種資訊以及各種新聞呢,且還能各種擴展,最主要是能獨立修改配置。終於屌絲的我在周末不斷嘗試后,終於使用爬蟲+ci定時觸發的方式搞定了,用起來還不錯。有興趣同學可以嘗試下
效果如下:
圖1
接下來給大家講講如何實現。

實現框架

該功能根據功能拆分,分為三個步驟,分別為python微博爬蟲以及數據處理、企業微信機器人數據發送,CI定時構建出發

  • 微博爬蟲
    1、python獲取10條微博最新資訊
    2、bs4包對最新的數據網頁進行解析
    3、過濾當前微博推廣的熱搜
    4、構建適企業微信機器人markdown數據
  • 企業微信機器人
    1、實現根據企業微信機器人接口進行請求方法
  • CI定時觸發
    1、使用coding提供的免費ci構建執行python腳本
    2、設置該構建job的定時任務

具體實現

python實現腳本

  • 數據解析以及組裝
    根據微博首頁地址“https://s.weibo.com/top/summary?cate=realtimehot” ,分析對我們有用的數據字段,我們可以根據標簽以及屬性的方式找到相關的關鍵字,同時定義一條微博結構應該包含如下幾個部分:
    1、ranking 排行
    2、message 具體的消息內容
    3、url 該條消息的超鏈接地址
    4、star 點贊數
    剩下的就可以通過該方式獲取微博數據,具體的代碼實現如下:
    圖1
    圖1
    def get_hot_message_from_weibo(self,message_num=10):
        '''
        從https://s.weibo.com/top/summary?cate=realtimehot獲取熱搜,熱搜一小時更新一次
        :return: 返回字典類型,{title:標題,girlurl:圖片地址
        '''
        self.url = "https://s.weibo.com/top/summary?cate=realtimehot"
        htmltext=self.get(self.url).text
        soup = BeautifulSoup(htmltext,'html.parser')
        hothtmls=soup.findAll(name="tr", attrs={"class" :""})
        hotsum = len(hothtmls)
        #傳入長度超過熱搜長度,返回為空
        hotmessages = []
        if message_num > hotsum:
            return dict()
        for hothtml in hothtmls:
            if hothtml.find(name='td',attrs={"class" :"td-01"}).text.strip()=='' or not hothtml.find(name='td',attrs={"class" :"td-01"}).text.strip().isdigit():
                continue
            hotmessage = dict()
            hotmessage['ranking'] = hothtml.find(name='td', attrs={"class": "td-01"}).text
            hotmessage['message'] = hothtml.find(name='a').text
            hotmessage['url'] = 'https://s.weibo.com/'+hothtml.find(name='a').attrs['href']
            hotmessage['star'] = hothtml.find(name='span').text
            hotmessages.append(hotmessage)
            #熱點消息支持10條
            message_num-=1
            if message_num <1:
                break
        return hotmessages
  • 數據轉markdown格式
    我先嘗試在微信機器人修改好markdown的格式,該部分需要大家自由發揮格式,再把相應的格式轉換為python腳本。具體的腳本參考如下:
    圖1
    def deal_message_to_markdown(self,hotmessages=None):

        '''
        處理各種途徑消息
        :param messages:
        :return:
        '''
        message='### 實時熱搜\r\n\
<font color="comment">時間:%s</font>\r\n'%time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
        for hotmessage in hotmessages:
            message+='> [%s.%s](%s) <font color=\"warning\">%s</font>\r\n'%(hotmessage['ranking'],
                                             hotmessage['message'],
                                             hotmessage['url'],
                                             hotmessage['star'])
        return message

企業微信發送

內容比較簡單,直接參考代碼

    def send_wechart(self,wechart_url,msg=None):
        '''
        企業微信發送圖片資源消息
        :param wechart_url: 企業微信機器人地址
        :param msg: 發送資源消息
        :return:
        '''
        header = {
            'Content-Type': 'application/json'
        }
        data = {
            "msgtype": "markdown",
            "markdown": {
                "content": msg
            }
        }
        respone = requests.post(url=wechart_url, headers=header, json=data)
        return respone

定時執行腳本CI

  • 進入coding平台(https://coding.net/) ,創建項目
  • 上傳代碼到git倉庫
    圖4
  • 進入持續集成平台,創建ci job
    圖5
  • 復制pipline腳本到構建任務
    圖6
  • 設置定時構建
    圖6

效果如下:

圖6


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM