通过第三方库qqbot来实现,pip install qqbot 直接安装就行
-
脚本直接运行会弹出二维码,手机qq扫码登录即可
-
通过#weather# city 来查询天气预报
-
通过#study# key value 来进行学习,以json格式存储在指定文件
-
第一次运行没有这个json文件,可以手动创建一个,内容为一对英文的双引号即可
-
通过#get# key 来进行获取之前存储的内容
-
只能在qq群进行自动操作
-
qqbot其他相关功能能查看 https://github.com/pandolia/qqbot/
|
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
|
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import
requests
import
qqbot
import
json
mess
=
json.load(
open
(
'qq.txt'
,
'r'
))
def
weather(city):
r
=
city
url
=
'http://www.sojson.com/open/api/weather/json.shtml?city='
+
city
html
=
requests.get(url)
result
=
json.loads(html.text)
if
result.get(
'status'
) !
=
200
:
return
"暂无【%s】天气预报!"
%
city
res
=
result.get(
'data'
).get(
'forecast'
)
for
i
in
res:
r
+
=
'\r\n'
+
i.get(
'date'
)
+
' '
+
i.get(
'type'
)
+
' '
+
i.get(
'low'
)
+
' '
+
i.get(
'high'
)
+
' '
+
i.get(
'fengli'
)
+
' '
+
i.get(
'fengxiang'
)
return
r
@qqbot
.QQBotSlot
def
onQQMessage(bot, contact, member, content):
if
"@ME"
in
content:
con
=
'圈我干啥??'
bot.SendTo(contact, con)
elif
content.startswith(
"#study#"
)
and
'#get#'
not
in
content
and
len
(content.split()) >
=
3
:
key,
*
value
=
content.split()[
1
:]
mess[key]
=
value
con
=
"录入成功!"
json.dump(mess,
open
(
'qq.txt'
,
'w'
))
bot.SendTo(contact, con)
elif
content.startswith(
'#get#'
)
and
len
(content.split()) >
=
2
:
key
=
content.split()[
1
]
con
=
mess.get(key)
if
key
in
mess
else
'不存在'
bot.SendTo(contact,
' '
.join(con))
elif
content.startswith(
"#weather#"
)
and
len
(content.split()) >
=
2
:
con
=
weather(content.split()[
1
])
bot.SendTo(contact, con)
if
__name__
=
=
'__main__'
:
qqbot.RunBot()
|
github地址:https://github.com/babyshen/Python/blob/master/qq%E6%9C%BA%E5%99%A8%E4%BA%BA.py
本文转自 baby神 51CTO博客,原文链接:http://blog.51cto.com/babyshen/1950812,如需转载请自行联系原作者