我正在尝试使用py2neo将我的neo4J数据库与Python连接。
我收到错误:
追踪(最近的呼叫最后):
文件“TwitterScraper.py”,第127行,中 insert_user_with_friends( 'ABC',[ “CCCC”])
在insert_user_with_friends中输入文件“TwitterScraper.py”,第93行 friend = friends.next()
UnboundLocalError:在赋值之前引用的局部变量“friends”
仍然无法正常工作。我使用的是Python 2.7.12和neo4j 3 x。这是我的代码(我跳过凭证和neo4j密码):
import time
import sys
####
# Get tweepy set up
import tweepy
from tweepy import Cursor
consumer_key="b"
consumer_secret=""
access_token=""
access_token_secret=""
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
####
from py2neo import Graph, authenticate
authenticate("localhost:7474", "neo4j", "")
graph_db = Graph("http://localhost:7474/db/data")
####
try:
graph_db.cypher.execute("""
CREATE CONSTRAINT ON (u:User)
ASSERT u.id_str IS UNIQUE
""")
except:
pass
def create_or_get_node(twitter_user,labels=[]):
data = {'id_str': twitter_user.id_str,
'name': twitter_user.name,
'screen_name': twitter_user.screen_name,
'description': twitter_user.description,
'url': twitter_user.url,
'followers_count': twitter_user.followers_count,
'friends_count': twitter_user.friends_count,
'listed_count': twitter_user.listed_count,
'statuses_count': twitter_user.statuses_count,
'favourites_count': twitter_user.favourites_count,
'location': twitter_user.location,
'time_zone': twitter_user.time_zone,
'utc_offset': twitter_user.utc_offset,
'lang': twitter_user.lang,
'profile_image_url': twitter_user.profile_image_url,
'geo_enabled': twitter_user.geo_enabled,
'verified': twitter_user.verified,
'notifications': twitter_user.notifications,
}
query_string = """
MERGE (u:User {id_str:{id_str}})
ON CREATE SET
"""+ (('u:'+',u:'.join(labels)+",") if labels else '') +"""
u.name={name},
u.screen_name={screen_name},
u.description={description},
u.url={url},
u.followers_count={followers_count},
u.friends_count={friends_count},
u.listed_count={listed_count},
u.statuses_count={statuses_count},
u.favourites_count={favourites_count},
u.location={location},
u.time_zone={time_zone},
u.utc_offset={utc_offset},
u.lang={lang},
u.profile_image_url={profile_image_url},
u.geo_enabled={geo_enabled},
u.verified={verified},
u.notifications={notifications}
""" + (("ON MATCH SET\n u:"+',u:'.join(labels)) if labels else '') +"""
RETURN u
"""
n=graph_db.cypher.execute_one(query_string, parameters=None, **data)
return n
def insert_user_with_friends(twitter_user,user_labels=[]):
user_labels.append("SeedNode")
if isinstance(twitter_user, str):
try:
twitter_user = api.get_user(twitter_user)
except:
time.sleep(60 * 16)
friend = friends.next()
create_or_get_node(twitter_user,user_labels)
friend_count = 0
print(u"\nINSERTING FOR: {}".format(twitter_user.name))
friends = Cursor(api.friends, user_id=twitter_user.id_str, count=200).items()
try:
while True:
try:
friend = friends.next()
except tweepy.TweepError:
print("exceeded rate limit. waiting")
time.sleep(60 * 16)
friend = friends.next()
#print u" INSERTING: {}".format(friend.name)
friend_count += 1
sys.stdout.write('.')
if(friend_count%10 == 0): sys.stdout.write(' ')
if(friend_count%50 == 0): sys.stdout.write('| ')
if(friend_count%100 == 0): print
create_or_get_node(friend)
n=graph_db.cypher.execute_one("""
MATCH (user:User {id_str:{user_id_str}}),(friend:User {id_str:{friend_id_str}})
CREATE UNIQUE (user)-[:FOLLOWS]->(friend)
""", parameters=None, user_id_str=twitter_user.id_str, friend_id_str=friend.id_str)
except StopIteration:
print(u"\n Total Friend Count = {}".format(friend_count))
insert_user_with_friends('sssss',["bbb"])
仍然得到同样的错误。谢谢你的回复。
这是我第一次编写python代码来连接neo4j。