Scraping Python, ESPN Liga BBVA y OTRAS [Parte 2]steemCreated with Sketch.

in #spanish7 years ago

Hoy vamos a tener 1 solo código para las 4 ligas principales del mundo como lo son: Liga BBVA, La Bundesliga, La Premier y La serie A... mi meta es crear una Base de Datos interesante con lo mejor de estas 4 ligas, y el código de hoy nos acerca mas a nuestra meta.

Con el siguiente código vamos a ir dando forma a los campos que vamos a utilizar en una de las tablas de nuestra interesante Base de Datos:

import urllib2, re
from bs4 import BeautifulSoup
import re
from prettytable import PrettyTable

# url that we are scraping
urls = ["http://www.espnfc.us/english-premier-league/23/table" , "http://www.espnfc.us/spanish-primera-division/15/table" , "http://www.espnfc.us/german-bundesliga/10/table" , "http://www.espnfc.us/italian-serie-a/12/table"]
hh = 0
for url in urls:
 page = urllib2.urlopen(url)

 soup = BeautifulSoup(page, "lxml")


 table = soup.find('table')

 rows = table.find_all('tr')

 results = []

 for row in rows:
       table_headers = row.find_all('th')
       if table_headers:
           results.append([headers.get_text() for headers in table_headers])

       table_data = row.find_all('td')
       if table_data:
   results.append([data.get_text() for data in table_data])


 tabla_formateada = []

 nueva_form = []
 
 equipo_nombre = []
 posicion_eq = []
 #Listas para totales
 PJ_totales = []
 PG_totales = []
 PE_totales = []
 PP_totales = []
 Goles_Favor_totales = []
 Goles_contra_totales = []
 Goles_Diferencia = []
 Puntos_tot = []
#Listas para casa
 PJ_totales = []
 PG_totales = []
 PE_totales = []
 PP_totales = []
 Goles_Favor_totales = []
 Goles_contra_totales = []
#Listas para visita
 PJ_totales = []
 PG_totales = []
 PE_totales = []
 PP_totales = []
 Goles_Favor_totales = []
 Goles_contra_totales = []

 i = 0
 for datos  in results:
 
   if i >= 2:
       while ( u'\xa0'  or u'\n' or u'' )  in datos:
           if (u'')  in datos :
               datos.remove(u'')
           elif  u'\n'  in datos:
      datos.remove(u'\n')
   elif  u'\xa0'  in datos:
      datos.remove(u'\xa0')
      
       
       
       
       tabla_formateada.append(datos)

   i += 1        


 
 for h in tabla_formateada:
  
     posicion = h[0]
     posicion_eq.append(posicion)
       
   
     equipo_nom = h[1]
     equipo_nom = equipo_nom.strip()
     equipo_nombre.append(equipo_nom)
       
     #Listas para totales
     PJ_totales = []
     PG_totales = []
     PE_totales = []
     PP_totales = []
     Goles_Favor_totales = []
     Goles_contra_totales = []
       
     Goles_Dif = h[-2]
     Goles_Diferencia.append(Goles_Dif)
       
     Puntos = h[-1]
     Puntos_tot.append(Puntos)

       
     #Listas para casa
     PJ_totales = []
     PG_totales = []
     PE_totales = []
     PP_totales = []
     Goles_Favor_totales = []
     Goles_contra_totales = []
     #Listas para visita
     PJ_totales = []
     PG_totales = []
     PE_totales = []
     PP_totales = []
     Goles_Favor_totales = []
     Goles_contra_totales = []
     
 numero_items = len(posicion_eq)
  


 table = PrettyTable(["Posicion", "Equipo", "Puntos", "Dif_Gol"])
 i = 0
 
 for i in range(0,numero_items):
   
   table.add_row([posicion_eq[i], equipo_nombre[i], Goles_Diferencia[i] , Puntos_tot[i]])
   
   i += 1
 if hh == 0:
   print " "
   print " "

   print "                     Liga Premier"
 elif hh == 1:
   print "                     Liga BBVA"

 elif hh == 2:
   print "                     Bundesliga"

   
 elif hh == 3:
   print "                     Seie A"
   
 print table
 hh += 1
 print " "
 print " "


Hay listas que se pueden llenar pero no las he llenado por que lo pienso hacer cuando comencemos con nuestra Base de Datos, la salida del programa es la siguiente:


Liga Premier
+----------+----------------------+--------+---------+
| Posicion |        Equipo        | Puntos | Dif_Gol |
+----------+----------------------+--------+---------+
|    1     |   Manchester City    |   6    |    9    |
|    2     |       Chelsea        |   5    |    9    |
|    3     |  Manchester United   |   5    |    9    |
|    4     |       Everton        |   2    |    7    |
|    5     |      Hull City       |   2    |    6    |
|    6     |    Middlesbrough     |   1    |    5    |
|    7     |  Tottenham Hotspur   |   1    |    5    |
|    8     |       Arsenal        |   1    |    4    |
|    9     |    Leicester City    |   0    |    4    |
|    10    | West Bromwich Albion |   0    |    4    |
|    11    |      Liverpool       |   -1   |    4    |
|    12    |   West Ham United    |   -2   |    3    |
|    13    |       Burnley        |   -2   |    3    |
|    14    |     Swansea City     |   -2   |    3    |
|    15    |     Southampton      |   -2   |    2    |
|    16    |      Sunderland      |   -2   |    1    |
|    17    |    Crystal Palace    |   -2   |    1    |
|    18    |       Watford        |   -3   |    1    |
|    19    |   AFC Bournemouth    |   -3   |    1    |
|    20    |      Stoke City      |   -4   |    1    |
+----------+----------------------+--------+---------+


                    Liga BBVA
+----------+---------------------+--------+---------+
| Posicion |        Equipo       | Puntos | Dif_Gol |
+----------+---------------------+--------+---------+
|    1     |      Las Palmas     |   6    |    6    |
|    2     |      Barcelona      |   5    |    6    |
|    3     |     Real Madrid     |   4    |    6    |
|    4     |      Sevilla FC     |   2    |    4    |
|    5     |    Sporting Gijón   |   1    |    4    |
|    6     | Deportivo La Coruña |   1    |    4    |
|    7     |       Leganes       |   1    |    4    |
|    8     |        Eibar        |   0    |    3    |
|    9     |    Real Sociedad    |   -1   |    3    |
|    10    |        Málaga       |   0    |    2    |
|    11    |   Atletico Madrid   |   0    |    2    |
|    12    |      Villarreal     |   0    |    2    |
|    13    |        Alavés       |   0    |    2    |
|    14    |       Espanyol      |   -2   |    1    |
|    15    |       Osasuna       |   -2   |    1    |
|    16    |       Granada       |   -4   |    1    |
|    17    |      Real Betis     |   -4   |    1    |
|    18    |      Celta Vigo     |   -2   |    0    |
|    19    |   Athletic Bilbao   |   -2   |    0    |
|    20    |       Valencia      |   -3   |    0    |
+----------+---------------------+--------+---------+


                    Bundesliga
+----------+--------------------------+--------+---------+
| Posicion |          Equipo          | Puntos | Dif_Gol |
+----------+--------------------------+--------+---------+
|    1     |      Bayern Munich       |   6    |    3    |
|    2     |        FC Cologne        |   2    |    3    |
|    2     |      VfL Wolfsburg       |   2    |    3    |
|    4     |    Borussia Dortmund     |   1    |    3    |
|    4     |      Hertha Berlin       |   1    |    3    |
|    4     | Borussia Monchengladbach |   1    |    3    |
|    7     |   Eintracht Frankfurt    |   1    |    3    |
|    8     |      TSG Hoffenheim      |   0    |    1    |
|    8     |        RB Leipzig        |   0    |    1    |
|    10    |        Hamburg SV        |   0    |    1    |
|    10    |     FC Ingolstadt 04     |   0    |    1    |
|    12    |       SC Freiburg        |   -1   |    0    |
|    12    |     Bayer Leverkusen     |   -1   |    0    |
|    12    |          Mainz           |   -1   |    0    |
|    15    |        Schalke 04        |   -1   |    0    |
|    16    |     SV Darmstadt 98      |   -2   |    0    |
|    16    |       FC Augsburg        |   -2   |    0    |
|    18    |      Werder Bremen       |   -6   |    0    |
+----------+--------------------------+--------+---------+


                    Seie A
+----------+----------------+--------+---------+
| Posicion |     Equipo     | Puntos | Dif_Gol |
+----------+----------------+--------+---------+
|    1     |     Genoa      |   4    |    6    |
|    2     |    Juventus    |   2    |    6    |
|    3     |   Sampdoria    |   2    |    6    |
|    4     |    AS Roma     |   4    |    4    |
|    5     |   US Pescara   |   3    |    4    |
|    6     |     Napoli     |   2    |    4    |
|    7     |     Torino     |   3    |    3    |
|    8     | Chievo Verona  |   1    |    3    |
|    9     |     Lazio      |   0    |    3    |
|    10    |   Fiorentina   |   0    |    3    |
|    11    |    AC Milan    |   -1   |    3    |
|    12    |    Udinese     |   -2   |    3    |
|    13    |    Sassuolo    |   -2   |    3    |
|    14    |    Bologna     |   -3   |    3    |
|    15    |    Palermo     |   -1   |    1    |
|    16    |    Cagliari    |   -2   |    1    |
|    17    | Internazionale |   -2   |    1    |
|    18    |    Atalanta    |   -2   |    0    |
|    19    |    Crotone     |   -3   |    0    |
|    20    |     Empoli     |   -3   |    0    |
+----------+----------------+--------+---------+
 


Voy a hacer realmente corta estas entradas por que esoty un poco cansado creando el codigo, voy a recalcar algunas cosas del mismo:

Por ejemplo la linea urls = ["http://www.espnfc.us/english-premier-league/23/table" , "http://www.espnfc.us/spanish-primera-division/15/table" , "http://www.espnfc.us/german-bundesliga/10/table" , "http://www.espnfc.us/italian-serie-a/12/table"] que es importante si queremos agregar otras ligas, y la linea  equipo_nom = equipo_nom.strip() que es importante para formatear una cadena, Hasta aquí lo vamos a dejar por esta entrada en un rato subo  la otra...

Dios les Bendiga

Coin Marketplace

STEEM 0.19
TRX 0.13
JST 0.029
BTC 60855.42
ETH 3369.56
USDT 1.00
SBD 2.50