> 백엔드 개발 > 파이썬 튜토리얼 > 用Python写过哪些脑洞大开的小工具?

用Python写过哪些脑洞大开的小工具?

WBOY
풀어 주다: 2016-06-06 16:21:55
원래의
2301명이 탐색했습니다.

只用Python写个网络爬虫工具,是个CSDN博客下载工具。
因为公司网络限制,上网都是间接上的,坑爹啊。。所以看好的博客系列性的文章,又没办法随时想看就看,一篇一篇保存又太慢,所以就花了点时间,用python写了这个博客爬虫工具,爬取博主的所有博客
附Github链接:Kevinsss/csdn-spider · GitHub

回复内容:

脑洞 1 :知乎带逛网站

之前曾抓取过知乎的一些数据(已经停掉了,现在不做任何爬虫了),然后通过一个算法,自动判断一个回答是否是美女爆照回答(通过文字、图片比,问题下所有回答带图比,答案评论、问题评论文本相似度等十多条规则),并将爆照回答的图片聚合到一个网站 zhihu.photo,由于已经停掉了,截图如下:
用Python写过哪些脑洞大开的小工具?


脑洞 2 :微博自动报时

我的微博(weibo.com/duxu0805)之前会每小时自动发布一条微博,附带一张壁纸图片和一个链接,链接是微博附图的高清无水印版下载地址。
用Python写过哪些脑洞大开的小工具?

脑洞 3 :得到知乎上两人的关系图

为了找到在轮子哥、四万姐的关注链上游有没有我,将知乎用户的关系链画了一张很大的有向图,并得到如下结果:
用Python写过哪些脑洞大开的小工具? 用Python写过哪些脑洞大开的小工具?关于实现,可以参考我的博客:xlzd.me/2016/01/31/pyth


脑洞 4 :认识一下我的工资所处水平

为了找到我的工资在互联网行业处于一个什么水平,通过拉勾的几十万招聘数据重新认识了自己的水平。
结论如下:从拉勾招聘看互联网行业


脑洞 5 :不想动,用树莓派吧

还在上学的时候,寒假在家。晚上躺在沙发看电视,睡前 20 分钟需要去打开电热毯,不想动,就用树莓派、继电器和 Python 实现了一个邮件控制电热毯通电、断电的程序,睡前 20 分钟发一封邮件,电热毯就开了。


脑洞 6 :知乎用户整体概况

不多赘述,请直接看博客吧:一个知乎重度用户眼中的知乎


脑洞 7 :各式各样的爬虫

大到1024 / GitHub ,小到妹子图,各式各样的网站基本全部抓过了,曾经聚合过一个网站,由于不喜欢喝茶,也停了。


脑洞 8 :face知乎

通过关键词检索知乎用户,包括但不限于性别、地域、个人描述等。
用Python写过哪些脑洞大开的小工具?


其它

用 Python 做了好多好多事情,暂时想到这些值得分享的,如果想起来,再继续更新出来吧~ 用Python写过哪些脑洞大开的小工具?
为什么知乎不支持markdown语言
---
破百了就更新
---

这货叫做 树莓派镜子
他其实是由原子镜+LCD屏幕+树莓派+木头盒子 组成的
用Python写过哪些脑洞大开的小工具?
我们先来说原子镜
用Python写过哪些脑洞大开的小工具?

原子镜的学名其实叫做 单向透视玻璃,但是在淘宝上搜 原子镜 这个关键词会好些

单面镜其实是“双面镜”,单面镜前后两面的光学性质根本没有分别,它之所以能产生单面反光的效果,完全是因为镜的两面处于不同光度的环境所造成的。我们先比较普通镜子和单面镜的结构。普通镜子是把银镀在玻璃上制成的。单面镜也一样,可是在镀银时就只会镀“一半”。怎样可以镀“一半”呢?方法倒是有点巧妙:镀上去的银(不是真银,是其他金属)十分薄,使一半的光可以通过,另一半就被反射回去。在实际使用时,犯人会面向镜面,而且身处有强光的房间。因为光线充足,反射的光较多,犯人便会在镜中看见自己的影象。此时证人站在镜的另一边,一个光线很微弱的房间里。虽然证人房间里部分的光线也可以穿过单面反光镜,但由于光度很低,所以犯人便看不到证人,只能看到自己的影像。情形就好像在街灯的强光下,我们不能看见萤火虫一样,因为来自萤火虫的微弱光线被街灯的光所盖过了。因此所谓单面反光的现象,只是视觉上的错觉罢了! (摘自百度百科)

原子镜广泛应用于监狱、公检法机构审讯室、精神病医院、大学科研机构研究室、大型会议室等,可达到里面看不到外面,外面可看到里面的效果。


比如 审讯室
用Python写过哪些脑洞大开的小工具?
还有外国人的街头公厕
用Python写过哪些脑洞大开的小工具?

这种厕所上起来感受应该比较刺激

当然还有应用于酒(tou)店(pai)的,所以在网上也有很多卖小尺寸的原子镜,一般是10x10cm的一块10块钱,我这个是12x14cm的也是10块钱,但是邮费就要20,所以很划不来,据说现在有种单透膜,可以达到同样的效果,没有研究过。

接下来说 LCD屏幕

我使用的是深圳微信公司出的4英寸LCD屏,这个屏幕用起来很方便,直接插起来就可以用


正面
用Python写过哪些脑洞大开的小工具?
反面
用Python写过哪些脑洞大开的小工具?
和树莓派合体
用Python写过哪些脑洞大开的小工具?
效果
用Python写过哪些脑洞大开的小工具?

这块屏幕更详尽的介绍

然后树莓派

感觉这货没什么好说的,大家应该都知道,稍微说一下

这是一个迷人有趣的Linux电脑


树莓派(为学生计算机编程教育设计的一种卡片式电脑)编辑 Raspberry Pi(中文名为“树莓派”,简写为RPi,或者RasPi/RPi)是为学生计算机编程教育而设计,只有信用卡大小的卡片式电脑,其系统基于Linux。 随着Windows 10 IoT的发布,我们也将可以用上运行Windows的树莓派。 自问世以来,受众多计算机发烧友和创客的追捧,曾经一“派”难求。别看其外表“娇小”,内“心”却很强大,视频、音频等功能通通皆有,可谓是“麻雀虽小,五脏俱全”。


树莓派由注册于英国的慈善组织“Raspberry Pi 基金会”开发,Eben·Upton/埃·厄普顿为项目带头人。2012年3月,英国剑桥大学埃本·阿普顿(Eben Epton)正式发售世界上最小的台式机,又称卡片式电脑,外形只有信用卡大小,却具有电脑的所有基本功能,这就是Raspberry Pi电脑板,中文译名"树莓派"。这一基金会以提升学校计算机科学及相关学科的教育,让计算机变得有趣为宗旨。基金会期望这 一款电脑无论是在发展中国家还是在发达国家,会有更多的其它应用不断被开发出来,并应用到更多领域。在2006年树莓派早期概念是基于Atmel的 ATmega644单片机,首批上市的10000“台”树莓派的“板子”,由中国台湾和大陆厂家制造。

树莓派名字中的pi其实指的就是 python

所以我选择了 python 作为 镜子的编程语言

那么说说我用python在这上面干了些什么

事实上最开始我的策略使用一个全屏的网页浏览器显示一个 黑底白字 的网站

可是坑爹的是 我更不不会web技术

然后我就打算要不试试python吧,本来还觉得不爽又要新学一门语言

但是

python给我很大的惊喜

我并没有学习python的语法,而是看了两个例子就直接使用tkinter库开始编程

这就是python的优势: 简单

接触python到用tk写出第一个的界面,只用了1个小时的时间

就是一开始那个环形显示时间那个

对于准大一新生来说,不管是python的语法还是库的使用都很容易,所以python很适合用来入门

我甚至打算教我的女朋友(nue)(gou)


接着我结合百度的天气API,pm2.5API等开发了一些其他界面
用Python写过哪些脑洞大开的小工具?

所有的界面都是黑底白字。


我觉得镜子展现的内容就应该简洁,所以界面上只有 数字 文字 线条 3个元素
用Python写过哪些脑洞大开的小工具?
后来我觉得只是这样太死气了
用Python写过哪些脑洞大开的小工具?

所以我开发了一个类,能让这些元素拥有动画效果,并且能让 Pm2.5 天气 时间 我和女朋友在一起了多少秒 等等 这些信息能够随机的在镜子上淡入淡出


最后我想,如果一定要跟镜子交互,那么语音应该是最好的选择,不然就是用智能手机链接镜子
用Python写过哪些脑洞大开的小工具?

我是比较偏向语音这个选择,因为这将会是一种新式的交互体验,就跟童话里的魔镜一样,我对这个镜子的定位是一个外形比较骚的公众信息聚合平台,所以语音相对于手机来说,更方便,而对于这样一个定位来讲,方便很重要,试想你在酒店想要找厕所,然后你问魔镜 ’魔镜魔镜,厕所在哪‘ 是不是比掏出手机链结魔镜,然后再用某种低效的输入进行交互方便的多?

再说百度等都有开放的API可以用何乐而不为?

所以下一步的目标就是把语音的功能开发出来,目前卡在了语音采集这个问题上。

还希望大神多指教

单位提供盒饭,不过需要自己前一天到内网的订餐页面订餐。有一天我忘了订餐,只好自己花钱订外卖了。

于是,为了避免这样的问题再发生,我写了一个订餐爬虫,实现了“一键订餐”。然后,使用Linux上的crontab服务,每个工作日的同一时间自动运行这个爬虫程序。

从此再也不用担心忘记订餐了。 把知乎的喜欢的答案扒下来做成电子书,并推送到kindle上看。

python爬虫 推送知乎文章到kindle电子书 python写了个命令行麻将
cugb1004101218/majiang · GitHub
半成品,不能碰,屁胡。 这个python小应用是我在Github上发现的,很有趣,实测可以运行
GitHub - 0x5e/wechat-deleted-friends: 查看被删的微信好友

实现的功能是查看把自己删了的微信好友,原理就是新建群组,用的是微信网页版的接口,如果加不进来就是被删好友了(不要在群组里讲话,别人是看不见的)


对了,这个用的是python2.x开发的,python3.x是运行不了的


这个是应用的Github界面

用Python写过哪些脑洞大开的小工具?

这个是运行之后的结果,会让你用微信扫一个二维码,然后就可以了

用Python写过哪些脑洞大开的小工具? 脑洞打开的小工具到没怎么写过,但是写了一个挑逗女神的一个小程序!!!
想看程序的 赶快来赞我吧!!! 抓取 MetArt 高清套图。感觉要掉节操 =_=

fetch.py:
<code class="language-python"><span class="c">#!/usr/bin/env python2.7</span>
<span class="c"># --*-- encoding: utf-8 --*--</span>

<span class="c"># enable print(,end='')</span>
<span class="c">#from __future__ import print_function </span>
<span class="kn">import</span> <span class="nn">argparse</span>
<span class="kn">import</span> <span class="nn">sys</span>
<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">re</span>
<span class="kn">import</span> <span class="nn">bs4</span>
<span class="kn">import</span> <span class="nn">urlparse</span>
<span class="kn">import</span> <span class="nn">requests</span>

<span class="k">def</span> <span class="nf">download</span><span class="p">(</span><span class="n">url</span><span class="p">,</span> <span class="n">out</span><span class="p">,</span> <span class="n">title</span><span class="p">):</span>
    <span class="c">#out = url.split('/')[-1]</span>
    <span class="n">resp</span> <span class="o">=</span> <span class="n">requests</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">url</span><span class="p">,</span> <span class="n">stream</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">resp</span><span class="o">.</span><span class="n">status_code</span> <span class="o">!=</span> <span class="n">requests</span><span class="o">.</span><span class="n">codes</span><span class="o">.</span><span class="n">ok</span><span class="p">:</span>
        <span class="k">return</span> <span class="bp">False</span>

    <span class="n">total</span> <span class="o">=</span> <span class="n">resp</span><span class="o">.</span><span class="n">headers</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">'content-length'</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">total</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
        <span class="n">total</span> <span class="o">=</span> <span class="mi">0</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">total</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">total</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">show_progress</span><span class="p">(</span><span class="n">total</span><span class="p">,</span> <span class="n">acc</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">total</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
            <span class="n">sys</span><span class="o">.</span><span class="n">stdout</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">"{} ?</span><span class="se">\r</span><span class="s">"</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">out</span><span class="p">))</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="c">#sys.stdout.write("{} [ {:3d}% ]\r".format(100*acc/total, out))</span>
            <span class="n">sys</span><span class="o">.</span><span class="n">stdout</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">"{} </span><span class="se">\"</span><span class="s">{}</span><span class="se">\"</span><span class="s"> {:3d}%</span><span class="se">\r</span><span class="s">"</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">title</span><span class="p">,</span> <span class="n">out</span><span class="p">,</span> <span class="mi">100</span><span class="o">*</span><span class="n">acc</span><span class="o">/</span><span class="n">total</span><span class="p">))</span>
        <span class="n">sys</span><span class="o">.</span><span class="n">stdout</span><span class="o">.</span><span class="n">flush</span><span class="p">()</span>

    <span class="n">acc</span> <span class="o">=</span> <span class="mi">0</span>
    <span class="n">show_progress</span><span class="p">(</span><span class="n">total</span><span class="p">,</span> <span class="n">acc</span><span class="p">)</span>
    <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">out</span><span class="p">,</span> <span class="s">'wb'</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
        <span class="k">for</span> <span class="n">chunk</span> <span class="ow">in</span> <span class="n">resp</span><span class="o">.</span><span class="n">iter_content</span><span class="p">(</span><span class="n">chunk_size</span><span class="o">=</span><span class="mi">1024</span><span class="p">):</span> 
            <span class="k">if</span> <span class="n">chunk</span><span class="p">:</span> <span class="c"># filter out keep-alive new chunks</span>
                <span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">chunk</span><span class="p">)</span>
                <span class="n">f</span><span class="o">.</span><span class="n">flush</span><span class="p">()</span>
                <span class="n">acc</span> <span class="o">+=</span> <span class="nb">len</span><span class="p">(</span><span class="n">chunk</span><span class="p">)</span>
                <span class="n">show_progress</span><span class="p">(</span><span class="n">total</span><span class="p">,</span> <span class="n">acc</span><span class="p">)</span>
    <span class="n">sys</span><span class="o">.</span><span class="n">stdout</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">"</span><span class="se">\n</span><span class="s">"</span><span class="p">)</span>

    <span class="k">return</span> <span class="bp">True</span>

<span class="k">def</span> <span class="nf">fetch</span><span class="p">(</span><span class="n">from_url</span><span class="p">,</span> <span class="n">offset</span><span class="p">,</span> <span class="n">count</span><span class="p">):</span>
    <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">from_url</span><span class="p">,</span> <span class="s">"r"</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
        <span class="n">html</span> <span class="o">=</span> <span class="n">f</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>
    <span class="n">soup</span> <span class="o">=</span> <span class="n">bs4</span><span class="o">.</span><span class="n">BeautifulSoup</span><span class="p">(</span><span class="n">html</span><span class="p">)</span>

    <span class="c"># get title</span>
    <span class="n">content</span> <span class="o">=</span> <span class="n">soup</span><span class="o">.</span><span class="n">find_all</span><span class="p">(</span><span class="s">"h2"</span><span class="p">,</span> <span class="n">class_</span><span class="o">=</span><span class="s">"first"</span><span class="p">)</span>
    <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">content</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
        <span class="k">print</span><span class="p">(</span><span class="s">"title not found!"</span><span class="p">)</span>
        <span class="k">return</span>
    <span class="k">elif</span> <span class="nb">len</span><span class="p">(</span><span class="n">content</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">1</span><span class="p">:</span>
        <span class="k">print</span><span class="p">(</span><span class="s">"title not unique!"</span><span class="p">)</span>

    <span class="n">fdir</span> <span class="o">=</span> <span class="n">content</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">contents</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="s">"utf8"</span><span class="p">)</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s">"–"</span><span class="p">,</span> <span class="s">"-"</span><span class="p">)</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="n">fdir</span><span class="p">):</span>
        <span class="n">os</span><span class="o">.</span><span class="n">mkdir</span><span class="p">(</span><span class="n">fdir</span><span class="p">)</span>
        <span class="k">print</span><span class="p">(</span><span class="s">"mkdir: </span><span class="se">\"</span><span class="s">{}</span><span class="se">\"</span><span class="s">"</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">fdir</span><span class="p">))</span>

    <span class="c"># get image url</span>
    <span class="n">content</span> <span class="o">=</span> <span class="n">soup</span><span class="o">.</span><span class="n">find_all</span><span class="p">(</span><span class="s">"div"</span><span class="p">,</span> <span class="nb">id</span><span class="o">=</span><span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="s">"^post-"</span><span class="p">))</span>
    <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">content</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
        <span class="k">print</span><span class="p">(</span><span class="s">"target not found!"</span><span class="p">)</span>
        <span class="k">return</span>
    <span class="k">elif</span> <span class="nb">len</span><span class="p">(</span><span class="n">content</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">1</span><span class="p">:</span>
        <span class="k">print</span><span class="p">(</span><span class="s">"target not unique!"</span><span class="p">)</span>

    <span class="n">imgs</span> <span class="o">=</span> <span class="n">content</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">find_all</span><span class="p">(</span><span class="s">"img"</span><span class="p">)</span>
    <span class="n">start</span> <span class="o">=</span> <span class="mi">0</span> <span class="k">if</span> <span class="n">offset</span> <span class="ow">is</span> <span class="bp">None</span> <span class="k">else</span> <span class="n">offset</span>
    <span class="n">end</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">imgs</span><span class="p">)</span> <span class="k">if</span> <span class="n">count</span> <span class="ow">is</span> <span class="bp">None</span> <span class="k">else</span> <span class="p">(</span><span class="n">start</span><span class="o">+</span><span class="n">count</span><span class="p">)</span>
    <span class="k">print</span><span class="p">(</span><span class="s">"# of image: {}"</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">nimg</span><span class="p">))</span>

    <span class="k">for</span> <span class="n">idx</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">start</span><span class="p">,</span> <span class="n">end</span><span class="p">):</span>
        <span class="n">img</span> <span class="o">=</span> <span class="n">imgs</span><span class="p">[</span><span class="n">idx</span><span class="p">]</span>
        <span class="n">url</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">img</span><span class="o">.</span><span class="n">attrs</span><span class="p">[</span><span class="s">"src"</span><span class="p">])</span>
        <span class="c">#url = url.replace("http://t10", "http://i10")</span>
        <span class="n">url</span> <span class="o">=</span> <span class="n">url</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s">"http://t"</span><span class="p">,</span> <span class="s">"http://i"</span><span class="p">)</span>
        <span class="n">ext</span> <span class="o">=</span> <span class="n">url</span><span class="p">[</span><span class="n">url</span><span class="o">.</span><span class="n">rfind</span><span class="p">(</span><span class="s">"."</span><span class="p">):]</span>
        <span class="n">out</span> <span class="o">=</span> <span class="s">"{0}/{1}{2}"</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">fdir</span><span class="p">,</span> <span class="n">idx</span><span class="o">+</span><span class="mi">1</span><span class="p">,</span> <span class="n">ext</span><span class="p">)</span>

        <span class="c"># get image file</span>
        <span class="k">for</span> <span class="n">retry</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">5</span><span class="p">):</span>
            <span class="n">title</span> <span class="o">=</span> <span class="p">(</span><span class="s">"({{:{}d}}/{{}})"</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">end</span><span class="p">)))</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">idx</span><span class="o">+</span><span class="mi">1</span><span class="p">,</span> <span class="n">end</span><span class="p">))</span>
            <span class="k">if</span> <span class="n">download</span><span class="p">(</span><span class="n">url</span><span class="p">,</span> <span class="n">out</span><span class="p">,</span> <span class="n">title</span><span class="p">):</span>
                <span class="k">break</span>
            <span class="k">print</span><span class="p">(</span><span class="s">"failed, try again ({})"</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">retry</span><span class="o">+</span><span class="mi">1</span><span class="p">))</span>


<span class="k">def</span> <span class="nf">main</span><span class="p">():</span>
    <span class="n">parser</span> <span class="o">=</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">(</span><span class="n">description</span><span class="o">=</span><span class="s">'nah'</span><span class="p">)</span>
    <span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'urls'</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="nb">str</span><span class="p">,</span> <span class="n">nargs</span><span class="o">=</span><span class="s">"+"</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s">'Specify URLs'</span><span class="p">)</span>
    <span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'--offset'</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="nb">int</span><span class="p">,</span> <span class="n">nargs</span><span class="o">=</span><span class="s">"?"</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s">'offset'</span><span class="p">)</span>
    <span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'--count'</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="nb">int</span><span class="p">,</span> <span class="n">nargs</span><span class="o">=</span><span class="s">"?"</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s">'count'</span><span class="p">)</span>
    <span class="n">args</span> <span class="o">=</span> <span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">()</span>
    <span class="k">for</span> <span class="n">url</span> <span class="ow">in</span> <span class="n">args</span><span class="o">.</span><span class="n">urls</span><span class="p">:</span>
        <span class="n">fetch</span><span class="p">(</span><span class="n">url</span><span class="p">,</span> <span class="n">args</span><span class="o">.</span><span class="n">offset</span><span class="p">,</span> <span class="n">args</span><span class="o">.</span><span class="n">count</span><span class="p">)</span>

<span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">"__main__"</span><span class="p">:</span>
    <span class="n">main</span><span class="p">()</span>
</code>
로그인 후 복사
写过一个把图片转换成字符画的小程序,效果如下 用Python写过哪些脑洞大开的小工具?
用Python写过哪些脑洞大开的小工具?
以下是代码:
<code class="language-python"><span class="kn">import</span> <span class="nn">Image</span>
 
<span class="n">color</span> <span class="o">=</span> <span class="s">'MNHQ$OC?7>!:-;.'</span> <span class="c">#zifu</span>
 
<span class="k">def</span> <span class="nf">to_html</span><span class="p">(</span><span class="n">func</span><span class="p">):</span>
    <span class="n">html_head</span> <span class="o">=</span> <span class="s">'''</span>
<span class="s">            </span>
<span class="s">              </span>
<span class="s">                <style type="text/css"></style></span>
<span class="s">                  body {font-family:Monospace; font-size:5px;}</span>
<span class="s">                </span>
<span class="s">              </span>
<span class="s">             '''</span>
    <span class="n">html_tail</span> <span class="o">=</span> <span class="s">''</span>
 <span class="c"># ding yi HTML</span>
    <span class="k">def</span> <span class="nf">wrapper</span><span class="p">(</span><span class="n">img</span><span class="p">):</span>
        <span class="n">pic_str</span> <span class="o">=</span> <span class="n">func</span><span class="p">(</span><span class="n">img</span><span class="p">)</span>
        <span class="n">pic_str</span> <span class="o">=</span> <span class="s">''</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">l</span> <span class="o">+</span> <span class="s">' <br>'</span> <span class="k">for</span> <span class="n">l</span> <span class="ow">in</span> <span class="n">pic_str</span><span class="o">.</span><span class="n">splitlines</span><span class="p">())</span>
        <span class="k">return</span> <span class="n">html_head</span> <span class="o">+</span> <span class="n">pic_str</span> <span class="o">+</span> <span class="n">html_tail</span>
 
    <span class="k">return</span> <span class="n">wrapper</span>
 <span class="c"># fan hui zhi</span>
<span class="nd">@to_html</span>
<span class="k">def</span> <span class="nf">make_char_img</span><span class="p">(</span><span class="n">img</span><span class="p">):</span>
    <span class="n">pix</span> <span class="o">=</span> <span class="n">img</span><span class="o">.</span><span class="n">load</span><span class="p">()</span>
    <span class="n">pic_str</span> <span class="o">=</span> <span class="s">''</span>
    <span class="n">width</span><span class="p">,</span> <span class="n">height</span> <span class="o">=</span> <span class="n">img</span><span class="o">.</span><span class="n">size</span>
    <span class="k">for</span> <span class="n">h</span> <span class="ow">in</span> <span class="nb">xrange</span><span class="p">(</span><span class="n">height</span><span class="p">):</span>
        <span class="k">for</span> <span class="n">w</span> <span class="ow">in</span> <span class="nb">xrange</span><span class="p">(</span><span class="n">width</span><span class="p">):</span>
            <span class="n">pic_str</span> <span class="o">+=</span> <span class="n">color</span><span class="p">[</span><span class="nb">int</span><span class="p">(</span><span class="n">pix</span><span class="p">[</span><span class="n">w</span><span class="p">,</span> <span class="n">h</span><span class="p">])</span> <span class="o">*</span> <span class="mi">14</span> <span class="o">/</span> <span class="mi">255</span><span class="p">]</span>
        <span class="n">pic_str</span> <span class="o">+=</span> <span class="s">'</span><span class="se">\n</span><span class="s">'</span>
    <span class="k">return</span> <span class="n">pic_str</span>
 
<span class="k">def</span> <span class="nf">preprocess</span><span class="p">(</span><span class="n">img_name</span><span class="p">):</span>
    <span class="n">img</span> <span class="o">=</span> <span class="n">Image</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="n">img_name</span><span class="p">)</span>
 
    <span class="n">w</span><span class="p">,</span> <span class="n">h</span> <span class="o">=</span> <span class="n">img</span><span class="o">.</span><span class="n">size</span>
    <span class="n">m</span> <span class="o">=</span> <span class="nb">max</span><span class="p">(</span><span class="n">img</span><span class="o">.</span><span class="n">size</span><span class="p">)</span>
    <span class="n">delta</span> <span class="o">=</span> <span class="n">m</span> <span class="o">/</span> <span class="mf">200.0</span>
    <span class="n">w</span><span class="p">,</span> <span class="n">h</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">w</span> <span class="o">/</span> <span class="n">delta</span><span class="p">),</span> <span class="nb">int</span><span class="p">(</span><span class="n">h</span> <span class="o">/</span> <span class="n">delta</span><span class="p">)</span>
    <span class="n">img</span> <span class="o">=</span> <span class="n">img</span><span class="o">.</span><span class="n">resize</span><span class="p">((</span><span class="n">w</span><span class="p">,</span> <span class="n">h</span><span class="p">))</span>
    <span class="n">img</span> <span class="o">=</span> <span class="n">img</span><span class="o">.</span><span class="n">convert</span><span class="p">(</span><span class="s">'L'</span><span class="p">)</span>
 
    <span class="k">return</span> <span class="n">img</span>
 
<span class="k">def</span> <span class="nf">save_to_file</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="n">pic_str</span><span class="p">):</span>
    <span class="n">outfile</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="s">'w'</span><span class="p">)</span>
    <span class="n">outfile</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">pic_str</span><span class="p">)</span>
    <span class="n">outfile</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
 
<span class="k">def</span> <span class="nf">main</span><span class="p">():</span>
    <span class="n">img</span> <span class="o">=</span> <span class="n">preprocess</span><span class="p">(</span><span class="nb">raw_input</span><span class="p">(</span><span class="s">'input your filename:'</span><span class="p">))</span>
    <span class="n">pic_str</span> <span class="o">=</span> <span class="n">make_char_img</span><span class="p">(</span><span class="n">img</span><span class="p">)</span>
    <span class="n">save_to_file</span><span class="p">(</span><span class="s">'char.html'</span><span class="p">,</span> <span class="n">pic_str</span><span class="p">)</span>
 
<span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">'__main__'</span><span class="p">:</span>
    <span class="n">main</span><span class="p">()</span>
</code>
로그인 후 복사
写过一个终端版的BT链接下载。。也不算脑洞大开。。
관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿