我已经安装了在R环境中运行python脚本所需的所有必需的头文件和库(python-dev)。我在Ubuntu 14.04上运行,它内置了python 2.7。正确安装“rPython”包(需要安装一些python库)。 完全相同的Python脚本在Python环境中运行良好。但是在尝试在R中运行时会抛出以下错误:
> library(rPython)
> python.load("twitter-geo-search.py", get.exception = TRUE)
Error in python.exec(code, get.exception) : name 'Twitter' is not defined
这是Python脚本:
#-----------------------------------------------------------------------
# twitter-search-geo
# - performs a search for tweets close to New Cross, and outputs
# them to a CSV file.
#-----------------------------------------------------------------------
from twitter import *
import sys
import csv
query = "#TheRevenant"
latitude = 39.0997 # geographical centre of search
longitude = -94.5783 # geographical centre of search
max_range = 1467 # search range in kilometres
num_results = 1500 # minimum results to obtain
outfile = "output.csv"
#-----------------------------------------------------------------------
# load our API credentials
#-----------------------------------------------------------------------
config = {}
execfile("config.py", config)
#-----------------------------------------------------------------------
# create twitter API object
#-----------------------------------------------------------------------
# Showing error from the below line:
twitter = Twitter(
auth = OAuth(config["access_key"], config["access_secret"], config["consumer_key"], config["consumer_secret"]))
#-----------------------------------------------------------------------
# open a file to write (mode "w"), and create a CSV writer object
#-----------------------------------------------------------------------
csvfile = file(outfile, "w")
csvwriter = csv.writer(csvfile)
#-----------------------------------------------------------------------
# add headings to our CSV file
#-----------------------------------------------------------------------
row = [ "user", "text", "latitude", "longitude" ]
csvwriter.writerow(row)
#-----------------------------------------------------------------------
# the twitter API only allows us to query up to 100 tweets at a time.
# to search for more, we will break our search up into 10 "pages", each
# of which will include 100 matching tweets.
#-----------------------------------------------------------------------
result_count = 0
last_id = None
while result_count < num_results:
#-----------------------------------------------------------------------
# perform a search based on latitude and longitude
# twitter API docs: https://dev.twitter.com/docs/api/1/get/search
#-----------------------------------------------------------------------
query = twitter.search.tweets(q = "", geocode = "%f,%f,%dkm" % (latitude, longitude, max_range), count = 100, max_id = last_id)
for result in query["statuses"]:
#-----------------------------------------------------------------------
# only process a result if it has a geolocation
#-----------------------------------------------------------------------
if result["geo"]:
user = result["user"]["screen_name"]
text = result["text"]
text = text.encode('ascii', 'replace')
latitude = result["geo"]["coordinates"][0]
longitude = result["geo"]["coordinates"][1]
# now write this row to our CSV file
row = [ user, text, latitude, longitude ]
csvwriter.writerow(row)
result_count += 1
last_id = result["id"]
#-----------------------------------------------------------------------
# let the user know where we're up to
#-----------------------------------------------------------------------
print "got %d results" % result_count
#-----------------------------------------------------------------------
# we're all finished, clean up and go home.
#-----------------------------------------------------------------------
csvfile.close()
print "written to %s" % outfile
也在Python中安装了所需的“twitter”包(这就是为什么它在Python环境中没有显示错误)。不知道为什么即使在那之后R也表现得像这样。
GitHub使用的Python脚本。链接here。