2013-04-28 83 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中的其他更改)。