代码在shell中运行,但不在模块中运行

时间:2013-05-02 11:36:10

标签: python json python-2.7 python-idle

当我在python shell中逐个输入命令时,从url下载json文件的代码运行正常。但是,当我尝试运行包含此代码的模块时,我得到:ValueError: No JSON object could be decoded。任何想法为什么会这样?我运行python 2.7。

import urllib2
from urllib2 import Request
import json
import re

url1 = "http://www.skyscanner.net/flights/lond/nyca/130514/130525/airfares-from-london-to-new-york-in-may-2013.html"

req = Request(url1)
res = urllib2.urlopen(req)
the_page = res.read()
theText = str(the_page)

myre = re.compile(r'"SessionKey":"((([a-z0-9]+-)+)[a-z0-9]{12})"')
match = re.search(myre, theText)

print match.group(1)

url2 = "http://www.skyscanner.net/dataservices/routedate/v2.0/"+str(match.group(1))
htmltext = urllib2.urlopen(url2)
data = json.load(htmltext)

现在整个代码:

import urllib2
from urllib2 import Request, urlopen, URLError, HTTPError
import json
import re


url1 = "http://www.skyscanner.net/flights/lond/nyca/130514/130525/airfares-from-london-to-new-york-in-may-2013.html"

req = Request(url1)
res = urllib2.urlopen(req)
the_page = res.read()
theText = str(the_page)

myre = re.compile(r'"SessionKey":"((([a-z0-9]+-)+)[a-z0-9]{12})"')
match = re.search(myre, theText)

url2 = "http://www.skyscanner.net/dataservices/routedate/v2.0/%s" % str(match.group(1))

req2 = urllib2.Request(url2)

try:
    response = urlopen(req2)
except HTTPError as e:
    print 'The server couldn\'t fulfill the request.'
    print 'Error code: ', e.code
except URLError as e:
    print 'We failed to reach a server.'
    print 'Reason: ', e.reason
else:
    data = json.loads(response.read())

print data["SessionKey"]

1 个答案:

答案 0 :(得分:0)

哈!你是对的,它是shell使用和脚本使用之间的区别。无论url1正在做什么,当你打开url2时,它还没有完成(远程)。等待时间很短,这一切都有效!

import time

url2 = "http://www.skyscanner.net/dataservices/routedate/v2.0/%s" % str(match.group(1))

time.sleep(5)

req2 = urllib2.Request(url2)

所以根本不是你的代码......哼哼!