Login Register






Reddit User Information (Python 3.3.x) filter_list
Author
Message
Reddit User Information (Python 3.3.x) #1
Imports:
Code:
import re from urllib import parse, request, error

Function:
Code:
def reddit_info(user): try: aurl = "http://www.reddit.com/user/" + user data = str(request.urlopen(aurl).read(), encoding="utf-8") trophies_matches = re.findall("trophy-name", data) trophies = [] num = len(trophies_matches) if num == 0: trophies.append("None") else: for i in range(1, num + 1): trophies.append(data.split("trophy-name\">")[i].split("<")[0]) output = "Submit Karma:\t" + data.split("<span class=\"karma\">")[1].split("<")[0] + "\r\n" output += "Comment Karma:\t" + data.split("<span class=\"karma comment-karma\">")[1].split("<")[0] +"\r\n" output += "Trophies:\r\n" for t in trophies: output += " - " + t + "\r\n" return output except Exception as e: return str(e)
Excuse my mixing of string manipulation and regular expression use (regarding the re.findall usage), I had originally started writing this with Regex but realized it wouldn't work, so I changed it.

Example Usage:
Code:
print(reddit_info("_vargas_"))

Example Output:
Code:
Submit Karma: 34,160 Comment Karma: 1,683,990 Trophies: - Gilding II - Three-Year Club - ComboCommenter - Team Periwinkle - reddit gold - Verified Email - ComboCommenter - ComboCommenter - ComboCommenter
[Image: CDUAq9d.png]

Reply





Messages In This Thread
Reddit User Information (Python 3.3.x) - by Shebang - 06-18-2014, 02:02 PM