ColombianTeamAnalysis
Attachment 'ubuntu-co_stats.py'
Download 1 #Python Version 2.5
2 #
3 #
4 #we need the following modules
5
6 import urllib2,string
7 from BeautifulSoup import BeautifulSoup
8
9
10 def main():
11 # parameter and constants
12 team_url='https://launchpad.net/~ubuntu-co/+members'
13
14 # open team's web page
15 team_page=urllib2.urlopen(team_url)
16
17 # Use BeautifulSoup to parse html
18 soup0 = BeautifulSoup(team_page)
19
20 #Find active members
21 p=soup0.find("table",{ "id":"activemembers"})
22
23 #Convert element to string and use BeautifulSoup again
24 soup1 = BeautifulSoup(str(p))
25
26 #Prepare output as a table
27 members_table=['<table class="listing sortable" id="activemembers"><thead><tr><th>Nombre</th><th>Ubuntero?</th><th>Karma</th></tr></thead><tbody>']
28
29 #Find all the ocurrences of <a href=""> in the member's table
30 member_data=soup1.findAll('a')
31
32 for i in member_data:
33 # get member's url
34 member_url='https://launchpad.net'+str(i.get('href'))
35 # Open member's web page
36 member_page=urllib2.urlopen(member_url)
37 # Use BeautifulSoup to parse html
38 soup2=BeautifulSoup(member_page)
39 #Check to see if the opened page belongs to a group, if it's not the case, get relevant data
40 q=soup2.find("a",{ "class":"menu-link-members"})
41 if q == None:
42 # add member data to output table
43 members_table.append('<tr><td>'+str(i).replace('href="','href="https://launchpad.net')+'</td><td>')
44 #find the word "Ubuntero" and if it says "Yes" or no, add relevant data to output table
45 u=soup2.find(text='Ubuntero:')
46 if u <> None:
47 v=u.findNext(text=True)
48 if v.find("Yes") > 0:
49 members_table.append('Si')
50 else:
51 members_table.append('No')
52 else:
53 members_table.append('No')
54 # Find Member's karma, add it to output table
55 karma=soup2.find("span",{ "id":"karma-total"}).contents[0]
56 members_table.append('</td><td>'+str(karma)+'</td></tr>')
57 # Add trailing info for table
58 members_table.append('</tbody></table>')
59 # Convert element to string
60 doc= ''.join(members_table)
61 # Use BeautifulSoup to parse html
62 soup = BeautifulSoup(doc)
63 # Print output table
64 print soup.prettify()
65
66 #call main function
67 main()
Attached Files
To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.You are not allowed to attach a file to this page.