Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我如何在NetworkX上创建一个图表,并在twitter上创建我的追随者?_Python_Networkx - Fatal编程技术网

Python 我如何在NetworkX上创建一个图表,并在twitter上创建我的追随者?

Python 我如何在NetworkX上创建一个图表,并在twitter上创建我的追随者?,python,networkx,Python,Networkx,我正在尝试使用networkx在python上创建一个图形。我想创建一个直接图,在节点上包含我和我的所有追随者,如果a跟随B,则在a和B之间有一条边。 我将追随者存储在一个名为result的数组中,但我不确定如何创建这个图。 下面是我下载追随者的代码 twitter_accounts = ["AccountA", "AccountB"] res = {} #res1 = {} follower = [] pbar = tqdm_notebook(total=len(twitter_account

我正在尝试使用networkx在python上创建一个图形。我想创建一个直接图,在节点上包含我和我的所有追随者,如果a跟随B,则在a和B之间有一条边。 我将追随者存储在一个名为result的数组中,但我不确定如何创建这个图。 下面是我下载追随者的代码

twitter_accounts = ["AccountA", "AccountB"]
res = {}
#res1 = {}
follower = []
pbar = tqdm_notebook(total=len(twitter_accounts))

for twitter_account in twitter_accounts:
    inner_structure = []
    for page in tweepy.Cursor(api.followers, screen_name=twitter_account,
                              skip_status=True, include_user_entities=False).items(10):
        val = page._json
        inner_dict = {}
        inner_dict["name"] = val["name"] 
        inner_dict["screen_name"] = val["screen_name"]
        if inner_dict not in inner_structure:
            inner_structure.append(inner_dict)

    res[twitter_account] = inner_structure

这是下面的部分

following = []
for twitter_account in twitter_accounts:
    user_ids = []
    inner_structure = []
    for page in tweepy.Cursor(api.friends, screen_name=twitter_account, skip_status=True, include_user_entities=False).items(10): #5000
        val = page._json
        inner_dict = {}
        inner_dict["name"] = val["name"]
        inner_dict["screen_name"] = val["screen_name"]

        if inner_dict not in inner_structure:
            inner_structure.append(inner_dict)
        #except:
        #    print("RateLimitError...waiting 15 minutes to continue")
        #    time.sleep(60 * 15)
        #    continue
        #break
    res1[twitter_account] = inner_structure
    pbar.update(1)    
pbar.close()



下面是我得到的图表的图片,这并不是我真正需要的

如果你关于链接/谁跟随谁的唯一信息是你名单上的每个人都跟随你,那么你的图表将是“星形的”,这可能没有那么有趣

您需要的基本信息包括:

将networkx导入为nx
graph=nx.graph()
对于twitter_帐户中的twitter_帐户:
graph.add_节点(twitter_帐户)
对于res[twitter_账户]中的关注者:
graph.add_节点(follower['name'])
添加边(关注者['name'],推特账户)
如果您想要一个有向图,您应该使用而不是图

编辑: 如果您有一个相同格式的附加结构
res1
,您只需继续向
图形添加节点和边即可(可以多次添加相同的节点和边-这将无效):

对于帐户,res1.items()中的跟随者:
图.添加节点(帐户)#确保这是一个节点
对于追随者中的追随者:
图.添加_节点(跟随器)
图.添加_边(跟随者、帐户)

希望这能解决问题。

这张图并不是我真正需要的。我不需要跟随AccountA和AccountB的人。我需要在两个帐户的追随者之间有一个边缘,如果他们彼此跟随,我的意思是如果AccountA后面跟着B和C,B和C也跟着彼此,我需要一个边缘,即使在B和C@Yuri之间。在这种情况下,听起来你需要为每个链接帐户发出tweepy.Cursor请求。在上面你发布的代码中,你只得到两个twitter账户的邻居的信息——而不是他们之间的关系(你有权访问这些信息吗?)但是我真的不知道如何将它们结合在一起形成一个图表,如果它们彼此跟随,那么这两个节点上的信息就有一条边@yuriwill会在当天晚些时候修改我的答案,除非有人之前这样做。。