求助 python 报错误 给予解决者 100 元话费

2014-05-31 15:24:46 +08:00
 chengxuan
AttributeError: 'NoneType' object has no attribute 'json'
6149 次点击
所在节点    Python
36 条回复
huyiwei
2014-05-31 15:28:18 +08:00
汗~明显变量没有赋值
phyng
2014-05-31 15:29:25 +08:00
楼主赶紧贴代码。。。
anheiyouxia
2014-05-31 15:36:27 +08:00
虽然我没学过Python,但是这个错误不是很明显吗?要么你没初始化,要么在传这个东西的时候就把这个产量搞丢了了

正确做法应该是直接看代码,看所有这个产量经过哪些操作,再看不懂,调试总可以了吧(如果你的IDE有调试功能的话)或者直接在所有用到这个产量的方法打印一下这个东西啊
Crossin
2014-05-31 15:43:40 +08:00
XXX.json
XXX为None
manfay
2014-05-31 15:45:27 +08:00
import json
xiandao7997
2014-05-31 15:46:48 +08:00
4楼对的,空对象找不到属性
xiaowangge
2014-05-31 15:46:56 +08:00
import json
binux
2014-05-31 15:48:47 +08:00
让我猜猜,你用的是requests?然后又自己封装了一层,应该返回 response object 的
然后,没有 returen 或者 return 为空了,外面的函数接着调用了 ret.json() 于是失败了?
RIcter
2014-05-31 15:51:42 +08:00
...这段错误要是有人能知道为什么报错..我给充话费..
zoowii
2014-05-31 16:13:34 +08:00
@RIcter up
zhy0216
2014-05-31 16:15:33 +08:00
@RIcter 我觉得8楼是对的
funagi
2014-05-31 16:19:33 +08:00
>>> None.json
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'json'
chengxuan
2014-05-31 16:26:15 +08:00
@huyiwei @Reply @Crossin @binux 跪谢各位大神!
我请求两个网站api。然后跑一会就报错挂啦。。
会不会是请求太快,api数据太大,导致我抓取不到数据,所以报错呢?
skydiver
2014-05-31 16:38:06 +08:00
@anheiyouxia 这么多产量。。。
liushuaikobe
2014-05-31 16:47:22 +08:00
@chengxuan
产生这个错误的场景太多太多了,没有context别人靠猜测根本没办法给你找出错误原因。你出100块钱就是像@RIcter 说的那样,看谁能猜对bug代码的上下文。

如果抓取是同步执行的,要么是API返回结果不是你想要的形式,要么是根本没访问到API就出Exception,print一下,一目了然。

如果抓取是异步的,那就是你没把对结果的处理写在callback里,发出请求后立即对result做处理,这是result还是None。
est
2014-05-31 16:52:20 +08:00
NullReferenceException NullPointerException
binux
2014-05-31 17:11:48 +08:00
@chengxuan 我继续猜
请求api封装的时候,为了防止异常,是这么写的

try:
__return requests.post('http://some.api.com/')
except:
__pass

当多次访问之后,requests 出现了
ConnectionError: HTTPConnectionPool(host='some.api.com', port=80): Max retries exceeded with url: /
异常,没有办法 return,于是,函数实际返回空

后面处理没有做判断,亦然调用 ret.json(),于是报错
anheiyouxia
2014-05-31 17:15:14 +08:00
@skydiver 手机打字,九宫格拼音,产量和变量呃于是242654264,不要太在意这些细节≥﹏≤
chengxuan
2014-05-31 17:34:24 +08:00
@liushuaikobe
@binux
@Crossin
源码贴出,跪谢各位大神。

#coding=utf-8
import time, re, requests, md5, urllib, urllib2, json ,sys ,os
#sys.path.append(os.path.dirname(__file__).replace('\\','/'))
from huobi import HuoBi
from bitfinex import Bitfinex
from configs import config,keys
class getprice(object):
def __init__(self):
#获取动态配置
self.huobi = HuoBi(keys['huobi']['key'],keys['huobi']['secretKey'])
self.bitf = Bitfinex(keys['bitf']['key'],keys['bitf']['secretKey'])
def buy_seller(self):
p1 = self.get_huobi()
pr1 = p1['sell']
p2 = self.get_bitf()
pr2 = p2['sell'] * config['rate']
if pr1 > pr2:
pre = (pr1 - pr2) / pr1 * 100
if pre >= config['agio']:
self.huobi_sell(p1['buy'])
self.bitf_sell('buy',p2['sell'])
else:
pre = (pr2 - pr1) / pr2 * 100
if pre >= config['agio']:
self.huobi_buy(p1['sell'])
self.bitf_sell('sell',p2['buy'])
print("huobi : %s %s finex : %s ") % (pr1,pre,pr2)
def get_huobi_seller(self):
try:
r = requests.get('http://market.huobi.com/staticmarket/depth_btc_json.js')
return r
except:
time.sleep(18)
self.get_huobi_seller()
def get_bitf_seller(self):
try:
r = requests.get('https://api.bitfinex.com/v1/book/btcusd')
return r
except:
time.sleep(18)
self.get_bitf_seller()
def get_huobi(self):
r1 = self.get_huobi_seller()
t = r1.json()
p1 = t['asks'][-5]
p2 = t['bids'][4]
pr1 = float(p1[0])
pr2 = float(p2[0])
return {'buy':pr2,'sell':pr1}
def get_bitf(self):
r2 = self.get_bitf_seller()
t = r2.json()
p1 = t['asks'][4]
p2 = t['bids'][4]
pr1 = float(p1['price'])
pr2 = float(p2['price'])
return {'buy':pr2,'sell':pr1}
def huobi_sell(self,price=''):
#getattr(huobi,method)
price = self.get_huobi()
price = price['buy']
info = self.huobi.sell(price,config['amount']) #{u'result': u'success', u'id': 18161354}
print(info)
def huobi_buy(self,price):
info = self.huobi.buy(price,config['amount'])
print(info)
def bitf_sell(self,side='sell',price=''):
"""
symbol (string): The name of the symbol (see `/symbols`).
amount (decimal): Order size: how much to buy or sell.
price (price): Price to buy or sell at. May omit if a market order.
exchange (string): "bitfinex".
side (string): Either "buy" or "sell".
type (string): Either "market" / "limit" / "stop" / "trailing-stop" / "fill-or-kill" / "exchange market" / "exchange limit" / "exchange stop" / "exchange trailing-stop" / "exchange fill-or-kill". (type starting by "exchange " are exchange orders, others are margin trading orders)
is_hidden (bool) true if the order should be hidden. Default is false.
"""
price = self.get_bitf()
price = price['buy']

params = {
"symbol":"btcusd",
"amount" : str(config['amount']),
"price" : str(price),
"exchange" : "bitfinex",
"side" : side,
"type" : "exchange market",
}

info = self.bitf.order_new(params)
print(info)
x = getprice()
while config['start']:
x.buy_seller()
time.sleep(18)
smallghost
2014-05-31 17:37:09 +08:00
这100元话费还是有作用的,我是打酱油的

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/115186

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX