使用py2n使用Python将twitter数据摄取到neo4J DB时出错

2024-05-13 02:46:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用py2neo将neo4jdb与Python连接起来。你知道吗

我得到一个错误:

Traceback (most recent call last):
File "TwitterScraper.py", line 127, in
insert_user_with_friends('abc',["cccc"])
File "TwitterScraper.py", line 93, in insert_user_with_friends
friend = friends.next()
UnboundLocalError: local variable 'friends' referenced before assignment

还是不行。我使用的是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


Tags: nameimportfriendidurllabelsifaccess