2012年5月5日 星期六

[python][json][urlopen][urllib2]ValueError: No JSON object could be decoded

raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded
前陣子再寫一個json crawler在測試時跑起來都滿OK的。
就把他加進去cron裏面跑,
結果他一直吐 No JSON object could be decoded。

問題
url透過 urlopen開啟再read()
好像會讓他讀不到內容。
註解掉的是舊的寫法,
可能再加入shell script後,read方面出了問題。
索性直接改成json.load(f)的方式。


def get_page(url):
#html=urllib2.urlopen(url).read()
#htmlx=simplejson.loads(html)
#htmlx=json.loads(html)
#120505 add json.load(f)
html=urllib2.urlopen(url)
htmlx=json.load(html)
return int(htmlx.get('page_all'))/10+1

Get json data via url and use in python (simplejson) - Stack Overflow
http://stackoverflow.com/questions/1640715/get-json-data-via-url-and-use-in-python-simplejson


沒有留言:

張貼留言