鱼C论坛

 找回密码
 立即注册
查看: 2674|回复: 3

[作品展示] python豆瓣用户关注网络

[复制链接]
发表于 2014-6-1 09:57:02 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
初学半个月,写了这样一个脚本,用于获取用户关注列表,组成网络(我只能说我是跟老师做社会网络,数据挖掘的)。发上来,给大家看看,希望大家能给点意见,初学,肯定有很多需要修改的地方。
  1. # python 3.4 gui
  2. import urllib.request
  3. import json
  4. import time
  5. # import easygui as g
  6. import sys

  7. '''
  8. 函数定义
  9. '''
  10. def user_follow(user_name):
  11.         api_key = '回复不可见' # 我申请的key
  12.         start_index = '1'
  13.         max_results = '50'#目前最大

  14.         '''
  15.         函数定义
  16.         '''
  17.         def connectfunction():
  18.                 succeedornot = 0
  19.                 while succeedornot == 0:
  20.                         try:
  21.                                 url_conn = urllib.request.urlopen('http://api.douban.com/people/' + user_name + '/contacts?apikey=' + api_key + '&alt=json&start-index=' + str(start_index) + '&max-results=' + str(max_results)) # 打开url
  22.                                 succeedornot = 1
  23.                         except (TimeoutError,urllib.error.URLError):
  24.                                 print('网络不稳定,正在重试...')
  25.                                 succeedornot = 0
  26.                 return url_conn
  27.                
  28.         File1 = connectfunction()
  29.         doubanHTML = File1.read().decode('utf-8') # 读入打开的url
  30.         doubanJSON = json.JSONDecoder().decode(doubanHTML) # 创建json

  31.         # 信息截取
  32.         # openSearch:itemsPerPage
  33.         itemsPerPage = doubanJSON['openSearch:itemsPerPage']['$t'] # 每次获取的关注人数
  34.        
  35.         # author
  36.         author = doubanJSON['author'] # 研究对象
  37.        
  38.         #         name
  39.         author_name = author['name']['$t'] # 用户名称

  40.         # openSearch:totalResults
  41.         totalResults = doubanJSON['openSearch:totalResults']['$t'] # 关注人数
  42.        
  43.         # 打印信息
  44.         print('关注人数:', totalResults)
  45.         print('每次获取的关注人数:', itemsPerPage)
  46.         print('对象名称:', author_name)

  47.         # file_name = 'E:\\' + author_name + '_follow_file.txt'
  48.         file_name = 'E:\\follow_file.txt'
  49.         follow_list_pair = []
  50.         follow_list_solo = []
  51.         follow_file = open(file_name,'a',encoding='utf-8')# 以写入模式打开,如果文件存在,则在末尾追加写入

  52.         '''
  53.         函数定义
  54.         '''
  55.         def milk_run(contact_list,contact_index):
  56.                 #        db:uid
  57.                 uid = contact_list[contact_index]['db:uid']['$t'] # 关注对象uid
  58.                
  59.                 #        db:title
  60.                 title = contact_list[contact_index]['title']['$t'] # 关注对象名称
  61.                
  62.                 return (title,uid)

  63.         '''
  64.         循环开始
  65.         '''
  66.         while int(start_index) <= int(totalResults): # 循环分段获取
  67.                 if int(start_index) + int(max_results) - 1 <= int(totalResults):
  68.                         end_index = int(start_index) + int(max_results) - 1 # 确定范围
  69.                         subtract = int(max_results)
  70.                 else:
  71.                         end_index = int(totalResults)
  72.                         subtract = end_index - int(start_index) + 1
  73.                 print(start_index,'-',end_index)
  74.                
  75.                 File1 = connectfunction()
  76.                 doubanHTML = File1.read().decode('utf-8') # 读入打开的url
  77.                 doubanJSON = json.JSONDecoder().decode(doubanHTML) # 创建json

  78.                 # entry
  79.                 entry = doubanJSON['entry'] # 关注对象列表

  80.                 for index in range(subtract):
  81.                         follow_list_pair.append(user_name + '        ' + milk_run(entry,index)[1] + '\n')# 存入列表
  82.                         follow_list_solo.append(milk_run(entry,index)[1])# 存入列表
  83.                         print('第' + str(int(start_index) + index) + '个关注:',milk_run(entry,index),',已保存')
  84.                
  85.                 start_index = int(start_index) + int(max_results) # 步进
  86.                 str(start_index)

  87.         follow_file.writelines(follow_list_pair)
  88.         follow_file.close()
  89.        
  90.         return follow_list_solo

  91. # user_input = g.enterbox(msg='请输入用户id或uid:', title='查询用户关注网络', default='ahbei', strip=True, image=None, root=None)       
  92. user_input = input('请输入用户名称:')
  93. done_user = []
  94. done_user.append(user_input)
  95. user_input_follow = user_follow(user_input)

  96. for followers in user_input_follow:# 二层
  97.         if str(followers) not in done_user:
  98.                 done_user.append(str(followers))
  99.                 user_follow_follow = user_follow(str(followers))
  100.                 # for followers_2 in user_follow_follow:# 三层
  101.                         # if str(followers_2) not in done_user:
  102.                                 # done_user.append(str(followers_2))
  103.                                 # user_follow_follow_follow = user_follow(str(followers_2))
  104.                                 # time.sleep(2) # 延迟2s
复制代码
后期会使用igraph进行网络分析(这个我以前用R语言玩过)
游客,如果您要查看本帖隐藏内容请回复

douban_gui.zip (1.66 KB, 下载次数: 7, 售价: 1 鱼币)

评分

参与人数 1荣誉 +10 鱼币 +10 贡献 +5 收起 理由
小甲鱼 + 10 + 10 + 5 感谢楼主无私奉献!

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2014-8-25 10:25:12 | 显示全部楼层
谢楼主分享
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-1-7 09:16:25 | 显示全部楼层
楼主,你好,我最近刚接触R语言的igraph但是很多不懂的地方,能不能请教你一下啊?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-1-7 14:11:11 | 显示全部楼层
学习学习
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-13 20:47

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表