Scoutdrago3

scrape evolutions

Sep 13th, 2015
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.19 KB | None | 0 0
  1. """Scrape evolutions"""
  2. from bs4 import BeautifulSoup
  3. import requests
  4.  
  5. levelUrl = "http://pokemondb.net/evolution/level"
  6. levelResponse = requests.get(levelUrl)
  7. levelHtml = levelResponse.content
  8. levelSoup = BeautifulSoup(levelHtml, 'html.parser')
  9. levelTable = levelSoup.find('table')
  10.  
  11. stoneUrl = "http://pokemondb.net/evolution/stone"
  12. stoneResponse = requests.get(stoneUrl)
  13. stoneHtml = stoneResponse.content
  14. stoneSoup = BeautifulSoup(stoneHtml, 'html.parser')
  15. stoneTable = stoneSoup.find('table')
  16.  
  17. noneUrl = "http://pokemondb.net/evolution/none"
  18. noneResponse = requests.get(noneUrl)
  19. noneHtml = noneResponse.content
  20. noneSoup = BeautifulSoup(noneHtml, 'html.parser')
  21.  
  22. """
  23. MAKE SURE TO IMPLEMENT HAPPINESS AND OTHER CONDITIONS WHEN I
  24. EXPAND THE AMMOUNT OF POKEMON TO 4XX
  25. """
  26.  
  27. def scrape(pokeName, levelTableArg=levelTable, stoneTableArg=stoneTable, nonePage=noneSoup):
  28.     """Scrape evolutions, running through multiple cases/pages"""
  29.     evolutions = []
  30.     if pokeName in levelTableArg.text:
  31.         for tr in levelTableArg.tbody.find_all('tr'):
  32.             tds = tr.find_all('td')
  33.             name = tds[0].text.strip()
  34.             if name == pokeName:
  35.                 pokemonLevel = tds[-2].text.strip()
  36.                 pokemonCondition = tds[-1].text.strip() or None
  37.                 evolutions.extend([str(pokemonLevel), str(pokemonCondition)])
  38.     elif pokeName in stoneTableArg.text:
  39.         for tr in stoneTableArg.tbody.find_all('tr'):
  40.             tds = tr.find_all('td')
  41.             name = tds[0].text.strip()
  42.             if name == pokeName:
  43.                 pokemonLevel = 0
  44.                 pokemonCondition = tds[-1].text.strip() or None
  45.                 evolutions.extend([str(pokemonLevel), str(pokemonCondition)])
  46.     elif pokeName in nonePage.text:
  47.         pokemonLevel = 101
  48.         pokemonCondition = None
  49.         evolutions.extend([str(pokemonLevel), str(pokemonCondition)])
  50.     else:
  51.         #This is for the pokemon that may need trading/other stuff
  52.         #that I may implement later, but for now, this is fine
  53.         pokemonLevel = 40
  54.         pokemonCondition = None
  55.         evolutions.extend([str(pokemonLevel), str(pokemonCondition)])
  56.  
  57.     return evolutions
Advertisement
Add Comment
Please, Sign In to add comment