好玩的Python技术有哪些

发布时间:2021-11-20 14:30:24 作者:iii
来源:亿速云 阅读:121

这篇文章主要介绍“好玩的Python技术有哪些”,在日常操作中,相信很多人在好玩的Python技术有哪些问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”好玩的Python技术有哪些”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

因为下面的代码都非常简单,简单到直接使用Python的交互式环境就能完成。当然,官方Python自带的交互式环境比较难用,推荐大家使用ipython,可以使用下面的命令来安装ipython,安装成功后键入ipython命令就能进入交互式环境。

pip install ipython

pip3 install ipython

ipython最直观的优点:

  1. 可以用?或者??来获取帮助。

  2. 可以用!调用系统命令。

  3. 可以使用Tab键自动补全。

  4. 可以使用魔法指令,如:%timeit。

没有工具用代码也能P图

1. 安装pillow三方库。

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

pip install pillow

pip3 install pillow

2 .加载图片。

from PIL import Image
chiling = Image.open('chiling.jpg')
chiling.show()

3 .使用滤镜。

from PIL import ImageFilter
chiling.filter(ImageFilter.EMBOSS).show()
chiling.filter(ImageFilter.CONTOUR).show()

4 .图像剪裁和粘贴。

rect = 220, 690, 265, 740 
watch = chiling.crop(rect)
watch.show()
blured_watch = watch.filter(ImageFilter.GaussianBlur(4))
chiling.paste(blured_watch, (220, 690))
chiling.show()

5 .生成镜像。

chiling2 = chiling.transpose(Image.FLIP_LEFT_RIGHT)
chiling2.show()

6 .生成缩略图。

width, height = chiling.size
width, height = int(width * 0.4), int(height * 0.4)
chiling.thumbnail((width, height))

7 .合成图片。

frame = Image.open('frame.jpg')
frame.show()
frame.paste(chiling, (210, 150))
frame.paste(chiling2, (522, 150))
frame.show()

向微信好友群fa祝福视频

1 .安装itchat三方库。

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

pip install itchat

pip3 install itchat

2 .登录微信。

import itchat
itchat.auto_login()

说明:用自己的微信扫描屏幕上出现的二维码就完成了登录操作,登录之后才能获取自己的好友信息以及发送消息给自己的好友。

3 .查找自己的朋友。

friends_list = itchat.get_friends(update=True)
print(len(friends_list))
luohao = friends_list[0]
props = ['NickName', 'Signature', 'Sex']
for prop in props:
 print(luohao[prop])

【说明】:friends_list相当于是一个列表,列表中的第一个元素是自己。

4 .随机选出5个朋友,获得他们的用户名、昵称、签名。

lucky_friends = random.sample(friends_list[1:], 5) 
props = ['NickName', 'Signature', 'City']
for friend in lucky_friends:
 for prop in props:
 print(friend[prop] or '没有此项信息') 
 print('-' * 80)

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

itchat.send_msg('急需一个红包来拯救堕落的灵魂!!!', toUserName='@8e06606db03f0e28d0ff884083f727e6')

6 .群fa视频给幸运的朋友们。

lucky_friends = random.sample(friends_list[1:], 5) 
for friend in lucky_friends:
 username = friend['UserName']
 itchat.send_video('/Users/Hao/Desktop/my_test_video.mp4', toUserName=username)

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

不用客户端查看热点新闻

  1. 安装requests库。

好玩的Python技术有哪些

pip install requests

pip3 install requests

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

import requests
resp = requests.get('http://api.tianapi.com/allnews/?key=请使用自己申请的Key&col=7&num=50')

说明:上面使用了天行数据提供的数据接口,需要的话可以自行去天行数据的网站注册开通,调用接口的时候要填写注册成功后系统分配给你的key。

3 .使用反序列化将JSON字符串解析为字典并获取新闻列表。

import json
newslist = json.loads(resp.text)['newslist']

4 .对新闻列表进行循环遍历,找到感兴趣的新闻,例如:华为。

for news in newslist:
 title = news['title']
 url = news['url']
 if '华为' in title:
 print(title)
 print(url)

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

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

到此,关于“好玩的Python技术有哪些”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

推荐阅读:
  1. 如何掌握Python技术 Python有哪些机器学习库
  2. 分享个好玩的算法游戏

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

python

上一篇:6个提高交互体验的JavaScript库分别是什么

下一篇:高性能WEB开发中如何加载JavaScript

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》