<class'urllib2.urlerror'=“”>:<urlopen error =“”[errno =“”2] =“”no =“”such =“”file =“”或=“”directory =“”> < /的urlopen> </类>

时间:2014-07-26 00:53:12

标签: python twitter scripting cgi

我想要做的是使用python cgi脚本在网页上显示推文。 这是我的代码。 此代码在终端中正常工作。但显示下面列出的错误。

#!/usr/bin/python
import cgitb
cgitb.enable(False, '/var/www/twitter/errors')

import oauth2
import time
import urllib2
import json

url1 = "https://api.twitter.com/1.1/search/tweets.json"  
params = {
    "oauth_version": "1.0",
    "oauth_nonce": oauth2.generate_nonce(),
    "oauth_timestamp": int(time.time())
}


consumer_key=''
consumer_secret=''
access_token=''
access_secret=''


consumer = oauth2.Consumer(key=consumer_key, secret=consumer_secret)
token = oauth2.Token(key=access_token, secret=access_secret)
params["oauth_consumer_key"] = consumer.key
params["oauth_token"] = token.key

prev_id = int("435458631669415936")

for i in range(1):
    url = url1
    params["q"] = "fifa"
    params["count"] = 15
#   params["geocode"] = ""
#   params["lang"] = "English"
    params["locale"] = "en"
    params["result_type"] = "popular" # Example Values: mixed, recent, popular
#   params["until"] = ""
#   params["since_id"] = ""

#   params["max_id"] = str(prev_id)
    req=oauth2.Request(method="GET",url=url,parameters=params)
    signature_method=oauth2.SignatureMethod_HMAC_SHA1()
    req.sign_request(signature_method,consumer,token)
    headers=req.to_header()
    url=req.to_url()
#   print headers
#   print url
    response=urllib2.Request(url)
    data=json.load(urllib2.urlopen(response))
    if data["statuses"] == []:
        print "end of data"
        break
    else:
        prev_id = int(data["statuses"][-1]["id"]) - 1
        print prev_id, i
    print data["statuses"]
    #f = open("outfile_" + str(i) + ".txt", "w")
    json.dump(data["statuses"], f)
    f.close()
    time.sleep(5)

这是我的代码,我正面临这个问题(我已经更改了安全的秘密和令牌):

global URLError = <class 'urllib2.URLError'>, err = error(2, 'No such file or     directory')

<class 'urllib2.URLError'>: <urlopen error [Errno 2] No such file or directory>
      args = (error(2, 'No such file or directory'),)
      errno = None
      filename = None
      message = ''
      reason = error(2, 'No such file or directory')
      strerror = None 

1 个答案:

答案 0 :(得分:0)

更改此行以查看网页内的错误。给定的日志记录目录可能不存在,或者存在但是网络服务器用户不可写:

cgitb.enable(True) # '/var/www/twitter/errors')