小编sun*_*nny的帖子

节点大小取决于NetworkX上的节点级别

我以.json文件的形式将我的Facebook数据导入到我的计算机上.数据格式如下:

{"nodes":[{"name":"Alan"},{"name":"Bob"}],"links":[{"source":0,"target:1"}]}
Run Code Online (Sandbox Code Playgroud)

然后,我使用这个功能:

def parse_graph(filename):
"""
Returns networkx graph object of facebook
social network in json format
"""
G = nx.Graph()
json_data=open(filename)
data = json.load(json_data)
# The nodes represent the names of the respective people
# See networkx documentation for information on add_* functions
G.add_nodes_from([n['name'] for n in data['nodes']])
G.add_edges_from([(data['nodes'][e['source']]['name'],data['nodes'][e['target']]['name']) for e in data['links']])
json_data.close()
return G
Run Code Online (Sandbox Code Playgroud)

使这个.json文件在NetworkX上使用图形.如果我找到节点的程度,我知道如何使用的唯一方法是:

degree = nx.degree(p)
Run Code Online (Sandbox Code Playgroud)

其中p是我所有朋友的图表.现在,我想绘制图形,使得节点的大小与该节点的程度相同.我该怎么做呢?

使用:

nx.draw(G,node_size=degree)
Run Code Online (Sandbox Code Playgroud)

没有工作,我想不出另一种方法.

python social-networking networkx python-2.7

18
推荐指数
1
解决办法
2万
查看次数

在praw中,我正在尝试打印评论正文,但如果我遇到空评论怎么办?

我试图打印来自subreddit顶部帖子的所有评论,以便我的机器人可以分析它们.我让它在当天早些时候运行,但我现在尝试运行它并且遇到了错误.

这是我的代码:

r = praw.Reddit('Comment crawler v1.0 by /u/...')
r.login('username', 'password')
subreddit=r.get_subreddit('subreddit')
post_limit = 25
subreddit_posts = subreddit.get_hot(limit=post_limit)
subids = set()
for submission in subreddit_posts:
    subids.add(submission.id)
subid = list(subids)

i=0
while i < post_limit:
    submission = r.get_submission(submission_id=subid[i])
    flat_comments = praw.helpers.flatten_tree(submission.comments)
    with open('alreadydone.txt', 'r') as f:
        already_done = [line.strip() for line in f]
    f.close()
    for comment in flat_comments:
        if "Cricketbot, give me Australian news" in **comment.body** and comment.id not in already_done:
            info = feedparser.parse(Australia) #Australia gives a link to an RSS …
Run Code Online (Sandbox Code Playgroud)

python reddit

7
推荐指数
1
解决办法
4620
查看次数

标签 统计

python ×2

networkx ×1

python-2.7 ×1

reddit ×1

social-networking ×1