Python零基础 请问为什么一直显示未定义text呢?

zzangguxi 2019-03-21 06:15:14
明明练习前面两个函数的时候分开运行都没问题,为什么结合之后就出错了呢?
...全文
403 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_20364589 2019-04-12
  • 打赏
  • 举报
回复
谢谢,已经解决了,有个新问题
# -*- coding: utf-8 -*-
import itchat
from itchat.content import TEXT
from itchat.content import *
import requests
from bs4 import BeautifulSoup
r=requests.get('https://s.weibo.com/top/summary?cate=realtimehot')
str1='今日微博热搜消息\n'
if(r.status_code==200):
r.encoding='utf-8'
bs=BeautifulSoup(r.text,features="lxml")
rank=bs.select('.td-02 a')
for index in range(10):
str1+=str(index+1)+'.'+rank[index].text.strip()+'https://s.weibo.com'+rank[index]['href']+'\n'

itchat.auto_login(hotReload=True)
itchat.send(u'Hello,world','filehelper')
groupName=u"测试群" # 你真实微信中的群备注名称.
@itchat.msg_register(TEXT, isGroupChat=True)
def SentChatRoomsMsg(name, context):
itchat.get_chatrooms(update=True)
iRoom = itchat.search_chatrooms(name)
for room in iRoom:
if room['NickName'] == name:
userName = room['UserName']
break
itchat.send_msg(context, userName)

SentChatRoomsMsg(groupName,str1)
print('发送完毕!')

Traceback (most recent call last):
File "D:\Python27\wxpy.py", line 29, in <module>
SentChatRoomsMsg(groupName,str1)
TypeError: 'NoneType' object is not callable
能指导下吗
qq_20364589 2019-04-09
  • 打赏
  • 举报
回复
# 天气预告
city = '××'
url = 'http://api.map.baidu.com/telematics/v3/weather?location=%s&output=json&ak=TueGDhCvwI6fOrQnLM0qmXxY9N0OkOiQ&callback=?'%city
# 使用requests发起请求,接受返回的结果
rs = requests.get(url)
# 使用loads函数,将json字符串转换为python的字典或列表
rs_dict = json.loads(rs.text)
# 取出error
error_code = rs_dict['error']
# 如果取出的error为0,表示数据正常,否则没有查询到结果
if error_code==0:
results=rs_dict['results']
# 根据索引取出天气信息字典
info_dict = results[0]
# 根据字典的key,取出城市名称
city_name = info_dict['currentCity']
# 取出pm值
#pm25 = info_dict['pm25']
#print('当前城市:%s pm值:%s'%(city_name,pm25))
# 取出天气信息列表
weather_data = info_dict['weather_data']
# for循环取出每一天天气的小字典
for weather_dict in weather_data:
# 取出日期,天气,风级,温度
date = weather_dict['date']
weather = weather_dict['weather']
wind = weather_dict['wind']
temperature = weather_dict['temperature']
abc = date+','+weather+','+temperature+','+wind
print abc
# print s
# 测试
# 特别备忘:下面中文字符串前面一定要记得加U或者用decode('utf-8')解码为unicode,这样系统会\
# 根据自我编码设置转换unicode.想不起来看自己博客上的博文详情。
to_who = u'信息部'
f=open('text.txt','r+')
msg=f.read().decode('gbk')+abc

time.sleep(2)
send_qq(to_who, msg)
i=3
while 1: #a="+"+str(i)
#i=i-1
send_qq(to_who, msg)
time.sleep(600)
f.close
你好,帮忙看看这段代码,总报错NameError: name 'abc' is not defined
Steven·简谈 2019-04-09
  • 打赏
  • 举报
回复
引用 4 楼 qq_20364589 的回复:
# 天气预告 city = '××' url = 'http://api.map.baidu.com/telematics/v3/weather?location=%s&output=json&ak=TueGDhCvwI6fOrQnLM0qmXxY9N0OkOiQ&callback=?'%city # 使用requests发起请求,接受返回的结果 rs = requests.get(url) # 使用loads函数,将json字符串转换为python的字典或列表 rs_dict = json.loads(rs.text) # 取出error error_code = rs_dict['error'] # 如果取出的error为0,表示数据正常,否则没有查询到结果 if error_code==0: results=rs_dict['results'] # 根据索引取出天气信息字典 info_dict = results[0] # 根据字典的key,取出城市名称 city_name = info_dict['currentCity'] # 取出pm值 #pm25 = info_dict['pm25'] #print('当前城市:%s pm值:%s'%(city_name,pm25)) # 取出天气信息列表 weather_data = info_dict['weather_data'] # for循环取出每一天天气的小字典 for weather_dict in weather_data: # 取出日期,天气,风级,温度 date = weather_dict['date'] weather = weather_dict['weather'] wind = weather_dict['wind'] temperature = weather_dict['temperature'] abc = date+','+weather+','+temperature+','+wind print abc # print s # 测试 # 特别备忘:下面中文字符串前面一定要记得加U或者用decode('utf-8')解码为unicode,这样系统会\ # 根据自我编码设置转换unicode.想不起来看自己博客上的博文详情。 to_who = u'信息部' f=open('text.txt','r+') msg=f.read().decode('gbk')+abc time.sleep(2) send_qq(to_who, msg) i=3 while 1: #a="+"+str(i) #i=i-1 send_qq(to_who, msg) time.sleep(600) f.close 你好,帮忙看看这段代码,总报错NameError: name 'abc' is not defined
把abc在那个for循环之前定义一下,不能凭空出现
zzangguxi 2019-03-21
  • 打赏
  • 举报
回复
引用 1 楼 L_cccC的回复:
第三个函数的text_filter写成了text.filter
啊是诶!晕嘞我看了好久都没看出来,谢谢啦!
流泪熊猫头 2019-03-21
  • 打赏
  • 举报
回复
从你的代码来看,text确实没有定义。问题出现在函数调用阶段,函数定义时不会检查你函数体内的变量是否定义的
L_cccC 2019-03-21
  • 打赏
  • 举报
回复
第三个函数的text_filter写成了text.filter

37,743

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • WuKongSecurity@BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧