SHOW:
|
|
- or go back to the newest paste.
| 1 | - | from bs4 import BeautifulSoup |
| 1 | + | def getVols(): |
| 2 | from bs4 import BeautifulSoup | |
| 3 | - | import urllib2 #http://love-python.blogspot.nl/2008/02/get-html-source-of-url.html |
| 3 | + | |
| 4 | import urllib2 #http://love-python.blogspot.nl/2008/02/get-html-source-of-url.html | |
| 5 | - | url = 'http://services.runescape.com/m=itemdb_rs/top100.ws' |
| 5 | + | url = 'http://services.runescape.com/m=itemdb_rs/top100.ws' |
| 6 | ||
| 7 | - | usock = urllib2.urlopen(url) |
| 7 | + | usock = urllib2.urlopen(url) |
| 8 | - | data = usock.read() |
| 8 | + | data = usock.read() |
| 9 | - | usock.close() |
| 9 | + | usock.close() |
| 10 | ||
| 11 | - | tr = BeautifulSoup(data).find_all('tbody')[0].find_all('tr')
|
| 11 | + | tr = BeautifulSoup(data).find_all('tbody')[0].find_all('tr')
|
| 12 | - | itnames, itids = {}, {}
|
| 12 | + | itnames, itids = {}, {}
|
| 13 | - | for thistr in tr: |
| 13 | + | for thistr in tr: |
| 14 | - | td = thistr.find_all('td')
|
| 14 | + | td = thistr.find_all('td')
|
| 15 | - | a = td[0].find_all('a')[0]
|
| 15 | + | a = td[0].find_all('a')[0]
|
| 16 | - | id = a['href'][a['href'].index('obj=')+4:]
|
| 16 | + | id = a['href'][a['href'].index('obj=')+4:]
|
| 17 | - | val = td[5].string.rstrip() |
| 17 | + | val = td[5].string.rstrip() |
| 18 | - | if val[-1] == 'm': |
| 18 | + | if val[-1] == 'm': |
| 19 | - | if val[-3:-1] == '.0': |
| 19 | + | if val[-3:-1] == '.0': |
| 20 | - | val = int(float(val[:-1])) |
| 20 | + | val = int(float(val[:-1])) |
| 21 | else: | |
| 22 | - | val = float(val[:-1]) |
| 22 | + | val = float(val[:-1]) |
| 23 | - | else: |
| 23 | + | |
| 24 | - | val = int(float(val[:-1]) * 1000) |
| 24 | + | val = int(float(val[:-1]) * 1000) |
| 25 | - | itnames[a.string] = val |
| 25 | + | itnames[a.string] = val |
| 26 | - | itids[id] = val |
| 26 | + | itids[id] = val |
| 27 | - | print itnames # now contains all data in the format {"Cannonball":107.6,...}
|
| 27 | + | # itnames now contains all data in the format {"Cannonball":107.6,...}
|
| 28 | - | print itids # now contains all data in the format {"2":107.6,...} |
| 28 | + | # itidsnow contains all data in the format {"2":107.6,...}
|
| 29 | return [itnames, itids] |