2013-04-28 48 views
1

这不是我的代码,它是我在互联网上发现的执行(或应该执行)我想要的任务的模块。为什么在Python中这是“无效的语法”?

print '{' 
for page in range (1,4): 
    rand = random.random() 
    id = str(long(rand*1000000000000000000)) 
    query_params = { 'q':'a', 
     'include_entities':'true', 'lang':'en', 
     'show_user':'true', 
     'rpp': '100', 'page': page, 
     'result_type': 'mixed', 
     'max_id':id} 
    r = requests.get('http://search.twitter.com/search.json', 
       params=query_params) 
    tweets = json.loads(r.text)['results'] 
    for tweet in tweets: 
     if tweet.get('text') : 
      print tweet 
print '}' 
print 

Python的外壳似乎表明,误差为1个线路I知道很少的Python所以不知道为什么它不工作。

+4

您正在使用哪个版本的python?如果> = 3.0,使用'print()'(它现在是一个函数) – lifetimes 2013-04-28 16:36:52

+0

那个排序..那个位。现在我得到“ImportError:没有模块名为'请求'” – user1765369 2013-04-28 16:39:58

+0

检查[这个答案](http://stackoverflow.com/questions/11994337/need-help-installing-requests-for-python-3),看起来喜欢它可以解决你的问题与'请求'模块。 – raina77ow 2013-04-28 16:42:26

回答

4

这段代码是为Python 2.x编写的,但是在Python 3.x中(其中print现在是一个正确的函数)。用print(SomeExpr)代替print SomeExp来解决这个问题。

这是detailed description这个区别(以及3.x中的其他更改)。

相关问题