前两天在51cto看见某篇推荐博文,大概是一个Python的小程序可以读取微信撤回的信息。豆子比较感兴趣,在知乎和github上搜索了相关的源代码,发现原来实现起来非常的简单,他的核心是使用一个叫做itchat的模块,这个模块模拟微信网页版的登录过程,然后之后就可以很轻松读取和发送信息了。
第一次使用的时候可能对他的格式有点疑惑,但是写两个小例子之后就比较熟悉这些模块里面的函数了,可以很容易地对好友,群聊和公众号的各种类型的信息进行读取,存储,写入。完全可以发挥想象在这个平台上做很多操作。
网上有很多相关的简介和有趣的应用小程序,这些小程序大小不超过百行,但是功能都很有趣
http://itchat.readthedocs.io/zh/latest/#itchat
比较有趣的小程序有:
图灵机器人自动对话,你甚至可以直接绑定这个API和某人甚至某群聊,这样对方和你交流的时候,图灵机器人都会替你自动回复啦
http://itchat.readthedocs.io/zh/latest/tutorial/tutorial0/
网易云音乐播放器,消息群发,检测黑名单好友等等
https://itchat.readthedocs.io/zh/latest/tutorial/tutorial2/
读取微信撤回的信息等等
https://zhuanlan.zhihu.com/p/25689314
豆子依葫芦画瓢,也弄了个类似的,可以通过微信获取城市PM2.5的状态
源代码很简单:
微信发送城市的名称,获取之后,读取www.pm25.com上面的数据,然后把结果发给微信
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
import
urllib.request
from
time
import
ctime
from
bs4
import
BeautifulSoup
import
itchat
def
getPM25(cityname):
site
=
'http://www.pm25.com/'
+
cityname
+
'.html'
page
=
urllib.request.urlopen(site)
html
=
page.read();
soup
=
BeautifulSoup(html.decode(
"utf-8"
),
"html.parser"
)
city
=
soup.find(
class_
=
'bi_loaction_city'
)
# 城市名称
aqi
=
soup.find(
"a"
, {
"class"
,
"bi_aqiarea_num"
})
# AQI指数
quality
=
soup.select(
".bi_aqiarea_right span"
)
# 空气质量等级
result
=
soup.find(
"div"
,
class_
=
'bi_aqiarea_bottom'
)
# 空气质量描述
output
=
city.text
+
u
'AQI指数:'
+
aqi.text
+
u
'\n空气质量:'
+
quality[
0
].text
+
result.text
print
(output)
print
(
'*'
*
20
+
ctime()
+
'*'
*
20
)
return
output
itchat.auto_login(hotReload
=
True
)
Help
=
"""
友情提示:
请输入城市拼音获取天气结果,如果无法识别,自动返回首都记录
"""
itchat.send(
Help
,toUserName
=
'filehelper'
)
@itchat
.msg_register(itchat.content.TEXT)
def
getcity(msg):
if
msg[
'ToUserName'
] !
=
'filehelper'
:
return
print
(msg[
'Text'
])
cityname
=
msg[
'Text'
]
result
=
getPM25(cityname)
itchat.send(result,
'filehelper'
)
if
__name__
=
=
'__main__'
:
itchat.run()
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
Chongqing
重庆AQI指数:
45
空气质量:优
PM2.
5
浓度:
31
微克
/
立方米
击败了全国
70
%
的城市,目前城市排名
110
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
Fri Mar
24
09
:
57
:
49
2017
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
Xichang
西昌AQI指数:
25
空气质量:优
暂未统计PM2.
5
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
Fri Mar
24
09
:
58
:
21
2017
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
|