Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- import sys
- import urllib2
- from HTMLParser import HTMLParser
- from htmlentitydefs import name2codepoint
- user_id = 1293167 # tuss laszlo
- #user_id = 751482 # pshegger
- so_base_url = "http://stackoverflow.com/users/"
- class SOParser(HTMLParser):
- def __init__(self):
- HTMLParser.__init__(self)
- self.span_started = False
- self.repu_started = False
- self.username_started = False
- self.username = ""
- self.repu = 0
- def handle_starttag(self, tag, attrs):
- if tag == "span":
- self.span_started = True
- if tag == "a" and self.span_started:
- if len(attrs) > 0 and attrs[0][0] == "href" and attrs[0][1].endswith("?tab=reputation"):
- self.repu_started = True
- if tag == "h1":
- if len(attrs) > 0 and attrs[0][0] == "id" and attrs[0][1] == "user-displayname":
- self.username_started = True
- def handle_endtag(self, tag):
- if tag == "span":
- self.span_started = False
- if tag == "a":
- self.repu_started = False
- if tag == "h1":
- self.username_started = False
- def handle_data(self, data):
- if self.username_started:
- self.username += data
- if self.repu_started:
- self.repu = int(data)
- def handle_charref(self, name):
- if name.startswith('x'):
- c = unichr(int(name[1:], 16))
- else:
- c = unichr(int(name))
- if self.username_started:
- self.username += c
- def main():
- global user_id
- if len(sys.argv) > 1:
- if sys.argv[1] == "-h":
- print "%s [userid=%d]" % (sys.argv[0], user_id)
- return
- user_id = int(sys.argv[1])
- so_user_url = so_base_url + str(user_id)
- parser = SOParser()
- parser.feed(urllib2.urlopen(so_user_url).read())
- print "Username:", parser.username
- print "Reputation:", parser.repu
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement