《手牵手带你走进python世界》系列一

手牵手带你走进python圈

  • 什么是python?python能干什么?

    • Python是由Guido van Rossum于1989年底发明的一种解释型、面向对象、动态数据类型的高级程序设计语言。
    • python可以做 web全栈人工智能机器学习爬虫开发数据分析游戏开发自动化测试网络安全运维研发 等等工作。
  • python安装骚操作

    • 官网下载 https://www.python.org/ 点击 Downloads 选项卡

    • 选择相应的版本

    • 向下查找

    • 下载完成后,安装python解释器

    • 然后左键双击

    • 执行下一步

      avatar

    • 勾选安装

    • 安装进度

    • 安装成功

    • 至此程序安装结束

  • 工具都已经安装完成了,那我来玩一下微信。

    • 第一步

      # 安装模块 wxpy 和 pyecharts
      pip install wxpy
      pip install pyecharts
      
    • 第二步

      # 引入模块并且实例化
      from wxpy import *
      bot = Bot()
      
    • 生成饼图

      #计算你的微信中有多少男朋友,女朋友?
      from wxpy import Bot
      from pyecharts.charts import Pie
      from pyecharts import options as opts
      
      bot = Bot(cache_path=True)
      
      friends = bot.friends()
      
      data = {"boys":0,"girls":0,"others":0}
      
      for friend in friends:
          if friend.sex == 1:
              data['boys'] +=1
          elif friend.sex==2:
              data['girls'] +=1
          else:
              data['others'] +=1
      
          if friend.city == "绍兴":
              print(friend.name)
      # print(data)
      keys = data.keys()
      vals = data.values()
      pie  = (Pie().add('男女性别',[list(z) for z in zip(keys,vals)])
              .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}")))
      
      pie.render('sex.html') 
      
    • 根据地区确定有多少人

      from wxpy import *
      from pyecharts.charts import Map
      bot = Bot(cache_path=True)
      friends = bot.friends()
      dic = {}
      for friend in friends:
          if friend.province in dic.keys():
              dic[friend.province] += 1
          else:
              dic[friend.province] = 1
      
      map = (Map().add('朋友分布区域',[list(z) for z in zip(dic.keys(),dic.values())],'china'))
      map.render('area.html')
      
    • 获取头像

      from wxpy import *
      
      bot = Bot(cache_path=False)
      
      friends = bot.friends()
      
      for friend in friends:
          filename = 'img/' + friend.name + '.jpg'
          friend.get_avatar(filename)
      
    • 图灵机器人

      import requests
      import json
      import sys
      # 图灵机器人
      def tulingApi(msg):
          try:
              url = "http://openapi.tuling123.com/openapi/api/v2"
              dic = {
                  "reqType": 0,
                  "perception": {
                      "inputText": {
                          "text": msg
                      },
                  },
                  "userInfo": {
                      "apiKey": "d4cb14cce189493f99050005ce4a78dc",
                      "userId": "465330"
                  }
              }
              str = json.dumps(dic)
              data = requests.post(url, data=str)
              info = json.loads(data.text)
              return info['results'][0]['values']['text']
          except:
              return ''
      
      # 启动
      if __name__ == '__main__':
          msg = sys.argv[1]
          t = tulingApi(msg)
          print(t)
      
    • 微信群聊

      from  wxpy import *
      
      bot = Bot(cache_path=True)
      
      friends = bot.friends()
      
      @bot.register()
      def recv_msg(msg):
          print('收到',msg.text)
      
      embed()  # 注意这个
      
    • 微信单聊

      from wxpy import *
      
      bot = Bot()
      friend = bot.search('nicheng')[0]
      
      @bot.register()
      def recv_msg(msg):
          if msg.sender == friend:
              print('收到的信息',msg.text)
              return '自动回复'+ msg.text
      
      embed()
      
posted @ 2019-06-19 00:12  巫小诗  阅读(344)  评论(0编辑  收藏  举报