ipython最直观的优点:

    1. 可以用?或者??来获取帮助。
    2. 可以用!调用系统命令。
    3. 可以使用Tab键自动补全。
    4. 可以使用魔法指令,如:%timeit。
    1. 安装pillow三方库。

      PIL(Python Imaging Library)是Python平台事实上的图像处理标准库了。PIL功能非常强大,而API却非常简单易用。但是PIL仅支持到Python 2.7,而且很多年都没有人维护了,于是一群志愿者在PIL的基础上创建了兼容的版本,名字叫,除了支持Python 3.x还加入了很多有用且有趣的新特性。

      1. pip install pillow

      1. pip3 install pillow
    2. 加载图片。

      1. from PIL import Image
      2. chiling = Image.open('chiling.jpg')
      3. chiling.show()
    3. 使用滤镜。

      1. from PIL import ImageFilter
      2. chiling.filter(ImageFilter.EMBOSS).show()
      3. chiling.filter(ImageFilter.CONTOUR).show()
    4. 图像剪裁和粘贴。

      1. rect = 220, 690, 265, 740
      2. watch = chiling.crop(rect)
      3. watch.show()
      4. blured_watch = watch.filter(ImageFilter.GaussianBlur(4))
      5. chiling.paste(blured_watch, (220, 690))
      6. chiling.show()
    5. 生成镜像。

      1. frame = Image.open('frame.jpg')
      2. frame.show()
      3. frame.paste(chiling2, (522, 150))
      4. frame.show()

    上面的知识在Python-100-Days项目的中也有对应的内容。

    1. 安装itchat三方库。

      itchat是一个开源的微信个人号接口,使用Python调用微信从未如此简单。

      1. pip install itchat

      1. pip3 install itchat
    2. 登录微信。

      1. import itchat
      2. itchat.auto_login()
    3. 查找自己的朋友。

      1. friends_list = itchat.get_friends(update=True)
      2. print(len(friends_list))
      3. luohao = friends_list[0]
      4. props = ['NickName', 'Signature', 'Sex']
      5. for prop in props:
      6. print(luohao[prop])
    4. 随机选出5个朋友,获得他们的用户名、昵称、签名。

    5. 给朋友发送文字消息。

      1. itchat.send_msg('急需一个红包来拯救堕落的灵魂!!!', toUserName='@8e06606db03f0e28d0ff884083f727e6')
      1. lucky_friends = random.sample(friends_list[1:], 5)
      2. for friend in lucky_friends:
      3. itchat.send_video('/Users/Hao/Desktop/my_test_video.mp4', toUserName=username)

    利用itchat还能做很多事情,比如有好友给自己发了消息又撤回了,如果想查看这些被撤回的消息,itchat就可以做到(注册一个接收消息的钩子函数,请参考);再比如,有时候我们想知道某个好友有没有把我们删除或者拉入黑名单,也可以利用itchat封装的群聊功能,非好友和黑名单用户不会被拉入群聊,通过创建群聊函数的返回值就可以判定你和指定的人之间的关系。

    1. 安装requests库。(点击常看官方文档

      1. pip install requests

      1. pip3 install requests
    2. 爬取新闻数据或者通过API接口获取新闻数据。

      1. import requests
      2. resp = requests.get('http://api.tianapi.com/allnews/?key=请使用自己申请的Key&col=7&num=50')
    3. 使用反序列化将JSON字符串解析为字典并获取新闻列表。

      1. import json
      2. newslist = json.loads(resp.text)['newslist']
    4. 对新闻列表进行循环遍历,找到感兴趣的新闻,例如:华为。

    5. 调用短信网关发送短信到手机上,告知关注的新闻标题并给出链接。

      1. import re
      2. pattern = re.compile(r'https*:\/\/[^\/]*\/(?P<url>.*)')
      3. matcher = pattern.match(url)
      4. if matcher:
      5. url = matcher.group('url')
      6. resp = requests.post(
      7. url='http://sms-api.luosimao.com/v1/send.json',
      8. auth=('api', 'key-请使用你自己申请的Key'),
      9. data={
      10. 'mobile': '13548041193',
      11. 'message': f'发现一条您可能感兴趣的新闻 - {title},详情点击https://news.china.com/{url} 查看。【Python小课】'
      12. },
      13. timeout=10,
      14. )