Scraping Xbox Live

The ingredients in this BeautifulSoup are mechanize and ClientForm. You'll need to link your gamer tag to your Windows Passport at xbox.com before this will work.

from BeautifulSoup import BeautifulSoup
from mechanize import Browser

WINDOWS_PASSPORT_LOGIN = 'youremail@yourdomain.com'
WINDOWS_PASSPORT_PASSWD = 'yourpasswd'

FRIEND_TABLE_CLASS = 'XbcProfileTable XbcFriendsListTable'
GAMER_TAG_CLASS = 'XbcGamerTag'
GAMER_PRESENCE_CLASS = 'XbcGamerPresence'

br = Browser()
br.open('http://live.xbox.com/en-US/profile/Friends.aspx')
br.select_form(name='f1')
br['login'] = WINDOWS_PASSPORT_LOGIN
br['passwd'] = WINDOWS_PASSPORT_PASSWD
br.submit() # Submit login form.
br.select_form(name='fmHF')
response = br.submit() # Submit redirect form.
friend_list = response.read()

soup = BeautifulSoup(friend_list)
friend_table = soup.find('table', {'class': FRIEND_TABLE_CLASS})
for row in friend_table.contents[1:]: # Skip header row.
gamer_tag = row.find('td', {'class': GAMER_TAG_CLASS})
gamer_tag = str(gamer_tag.find('a').contents[0])
gamer_presence = row.find('td', {'class': GAMER_PRESENCE_CLASS})
gamer_presence = str(gamer_presence.find('h4').contents[0])
print gamer_tag, gamer_presence

2 comments:

  1. Bro, does this still work? I'm trying to get it working in php and it wont work. Help please.

    ReplyDelete
  2. Sorry, I haven't had a need for this in a long time. I'm not sure it still works. You'll probably need to tweak some stuff.

    ReplyDelete