Created
December 9, 2012 14:06
-
-
Save stephenLee/4245032 to your computer and use it in GitHub Desktop.
Renren friends collections
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html> | |
| <meta charset="utf-8"> | |
| <script src="http://d3js.org/d3.v2.min.js?2.9.3"></script> | |
| <style> | |
| .link { | |
| stroke: #ccc; | |
| } | |
| .node text { | |
| pointer-events: none; | |
| font: 10px sans-serif; | |
| } | |
| </style> | |
| <body> | |
| <script> | |
| var width = 960, | |
| height = 500 | |
| var svg = d3.select("body").append("svg") | |
| .attr("width", width) | |
| .attr("height", height); | |
| var force = d3.layout.force() | |
| .gravity(.05) | |
| .distance(100) | |
| .charge(-100) | |
| .size([width, height]); | |
| d3.json("friends.json", function(json) { | |
| force | |
| .nodes(json) | |
| .start(); | |
| var link = svg.selectAll(".link") | |
| .data(json) | |
| .enter().append("line") | |
| .attr("class", "link"); | |
| var node = svg.selectAll(".node") | |
| .data(json) | |
| .enter().append("g") | |
| .attr("class", "node") | |
| .call(force.drag); | |
| node.append("image") | |
| .attr("xlink:href", function(d){return d.head}) | |
| .attr("x", -8) | |
| .attr("y", -8) | |
| .attr("width", 16) | |
| .attr("height", 16); | |
| node.append("text") | |
| .attr("dx", 12) | |
| .attr("dy", ".35em") | |
| .text(function(d) { return d.name }); | |
| force.on("tick", function() { | |
| node.attr("transform", | |
| function(d){return "translate(" + d.x + ", " + d.y+ ")";}); | |
| }); | |
| }); | |
| </script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import requests | |
| import re | |
| import json | |
| import argparse | |
| from bs4 import BeautifulSoup | |
| def login_in(user, password): | |
| headers = { | |
| 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', | |
| 'Accept-Encoding': 'gzip, deflate', | |
| 'Accept-Language': 'en-us,en;q=0.5', | |
| 'Cache-Control': 'no-cache', | |
| 'Connection': 'keep-alive', | |
| 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', | |
| 'Host': 'www.renren.com', | |
| 'Pragma': 'no-cache', | |
| 'Referer': 'http://www.renren.com/SysHome.do', | |
| 'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:11.0) Gecko/20100101 Firefox/11.0' | |
| } | |
| payload = {'email': user, 'password': password} | |
| login_url = 'http://www.renren.com/ajaxLogin/login' | |
| r = requests.post(login_url, data=payload, headers=headers) | |
| return r.cookies | |
| def get_friends(user, password): | |
| cookies = login_in(user, password) | |
| friends_url = 'http://friend.renren.com/myfriendlistx.do#item_0' | |
| res = requests.get(friends_url, cookies=cookies) | |
| #print res.status_code, res.text | |
| m = re.search(r'var friends=(.*);', res.text) | |
| friends_list = m.group(1) | |
| friends = json.loads(friends_list) | |
| print "total friends: %s" % len(friends) | |
| with open('friends.json', 'w') as outfile: | |
| json.dump(friends, outfile) | |
| with open('friends.csv', 'w') as f: | |
| for friend in friends: | |
| f.write(str(friend['id'])) | |
| f.write(', ') | |
| f.write(friend['name'].encode('utf-8')) | |
| f.write(', ') | |
| f.write(friend['head']) | |
| f.write('\n') | |
| def get_friends_list(user, password, id): | |
| cookies = login_in(user, password) | |
| url = 'http://friend.renren.com/GetFriendList.do?id=%s' % id | |
| res = requests.get(url, cookies=cookies) | |
| soup = BeautifulSoup(res.text) | |
| #print soup.prettify() | |
| friends_list_tag = soup.find("ol",id="friendListCon") | |
| for friend in friends_list_tag: | |
| parser = argparse.ArgumentParser(description='Friends collections command line tool for Renren') | |
| parser.add_argument("user", help="your Renren username") | |
| parser.add_argument("password", help="your Renren password") | |
| parser.add_argument('id', help="the people's id you want to crawl") | |
| args = parser.parse_args() | |
| user = args.user | |
| password = args.password | |
| id = args.id | |
| #get_friends(user, password) | |
| get_friends_list(user, password, id) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment