ESPN BBVA, Scraping with Python [Part 1]steemCreated with Sketch.

in #programming7 years ago

I have finished putting the finishing touches to our first scraping in python on the ESPN Page.

I want to clarify that there are many ways to do things and I want to do them my way may not be the best but is the one I think simpler to explain.


As an introduction to this post I will tell you how I plan to do Scraping entries on this Blog.

First I'm going to do the data collection, let's talk a bit about the code that we use sometimes let's talk about code improvements.


Then we will generate the output as close to the page (all this in the 1st entry of each Scrap).


Secondly we are going to design and generate the database, in the first instance with SQLITE.

Third we will do comparative studies of the Database we will generate Charts and Searches.


To what pages will I do Scraping ????? '


I think that to start the Soccer standings, then with the table of players of each team, and with this we can generate other tables by filtration of the first 2, Graphs that do not go elsewhere here we will do them. Grain we are going to make SCRAPING to this page:  primera-division-de-espana


To generate interesting Graphs, we are going to do Scraping all of them from time to time this page and we are going to see some interesting graphs.


 Here's what the table looks like:

This is the table where we are going to get the information, and this our first code:

import urllib2
from bs4 import BeautifulSoup
import re

# url that we are scraping
url = "http://www.espn.com.ve/futbol/posicionesenvivo/_/liga/esp.1/primera-division-de-espana"

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])

print results


The code is a bit tangled:


First we connect with urllib2 with BeautifulSoup we make a soup "soup = BeautifulSoup (page," lxml ")".


Then we look for the tag table "table = soup.find ('table')"


Then we look for all rows = rows = table.find_all ('tr') "and create the blank list" results = [] "


We create a loop to traverse the rows stored in rows, in the loop we look for the Headers of the tables or "th" and if they exist we extract the text.


We do the same with the data of the tables or "td" and extract it with "get_text" at the end we print the result that is this:

[[u'30 de agosto de 2016Primera Divisi\xf3n de Espa\xf1a 2016/2017 Tabla de posiciones'], [u'\xa0', u'\xa0', u'\xa0', u'General', u'', u'Local', u'', u'Visitante'], [u'Pos.', u'', u'EQUIPO', u'Pts.', u'', u'PJ', u'G', u'E', u'P', u'GF', u'GC', u'Dif.', u'', u'G', u'E', u'P', u'GF', u'GC', u'', u'G', u'E', u'P', u'GF', u'GC'], [u'1', u'\n', u'Las Palmas', u'6', u'', u'2', u'2', u'0', u'0', u'9', u'3', u'6', u'', u'1', u'0', u'0', u'5', u'1', u'', u'1', u'0', u'0', u'4', u'2'], [u'2', u'\n', u'Barcelona', u'6', u'', u'2', u'2', u'0', u'0', u'7', u'2', u'5', u'', u'1', u'0', u'0', u'6', u'2', u'', u'1', u'0', u'0', u'1', u'0'], [u'3', u'\n', u'Real Madrid', u'6', u'', u'2', u'2', u'0', u'0', u'5', u'1', u'4', u'', u'1', u'0', u'0', u'2', u'1', u'', u'1', u'0', u'0', u'3', u'0'], [u'4', u'\n', u'Sevilla FC', u'4', u'', u'2', u'1', u'1', u'0', u'6', u'4', u'2', u'', u'1', u'0', u'0', u'6', u'4', u'', u'0', u'1', u'0', u'0', u'0'], [u'5', u'\n', u'Sporting Gij\xf3n', u'4', u'', u'2', u'1', u'1', u'0', u'2', u'1', u'1', u'', u'1', u'0', u'0', u'2', u'1', u'', u'0', u'1', u'0', u'0', u'0'], [u'6', u'\n', u'Deportivo La Coru\xf1a', u'4', u'', u'2', u'1', u'1', u'0', u'2', u'1', u'1', u'', u'1', u'0', u'0', u'2', u'1', u'', u'0', u'1', u'0', u'0', u'0'], [u'7', u'\n', u'Leganes', u'4', u'', u'2', u'1', u'1', u'0', u'1', u'0', u'1', u'', u'0', u'1', u'0', u'0', u'0', u'', u'1', u'0', u'0', u'1', u'0'], [u'8', u'\n', u'Eibar', u'3', u'', u'2', u'1', u'0', u'1', u'2', u'2', u'0', u'', u'1', u'0', u'0', u'1', u'0', u'', u'0', u'0', u'1', u'1', u'2'], [u'9', u'\n', u'Real Sociedad', u'3', u'', u'2', u'1', u'0', u'1', u'2', u'3', u'-1', u'', u'0', u'0', u'1', u'0', u'3', u'', u'1', u'0', u'0', u'2', u'0'], [u'10', u'\n', u'M\xe1laga', u'2', u'', u'2', u'0', u'2', u'0', u'3', u'3', u'0', u'', u'0', u'1', u'0', u'1', u'1', u'', u'0', u'1', u'0', u'2', u'2'], [u'11', u'\n', u'Atletico Madrid', u'2', u'', u'2', u'0', u'2', u'0', u'1', u'1', u'0', u'', u'0', u'1', u'0', u'1', u'1', u'', u'0', u'1', u'0', u'0', u'0'], [u'12', u'\n', u'Villarreal', u'2', u'', u'2', u'0', u'2', u'0', u'1', u'1', u'0', u'', u'0', u'1', u'0', u'0', u'0', u'', u'0', u'1', u'0', u'1', u'1'], [u'13', u'\n', u'Alav\xe9s', u'2', u'', u'2', u'0', u'2', u'0', u'1', u'1', u'0', u'', u'0', u'1', u'0', u'0', u'0', u'', u'0', u'1', u'0', u'1', u'1'], [u'14', u'\n', u'Espanyol', u'1', u'', u'2', u'0', u'1', u'1', u'6', u'8', u'-2', u'', u'0', u'1', u'0', u'2', u'2', u'', u'0', u'0', u'1', u'4', u'6'], [u'15', u'\n', u'Osasuna', u'1', u'', u'2', u'0', u'1', u'1', u'1', u'3', u'-2', u'', u'0', u'0', u'1', u'0', u'2', u'', u'0', u'1', u'0', u'1', u'1'], [u'16', u'\n', u'Granada', u'1', u'', u'2', u'0', u'1', u'1', u'2', u'6', u'-4', u'', u'0', u'1', u'0', u'1', u'1', u'', u'0', u'0', u'1', u'1', u'5'], [u'17', u'\n', u'Real Betis', u'1', u'', u'2', u'0', u'1', u'1', u'2', u'6', u'-4', u'', u'0', u'1', u'0', u'0', u'0', u'', u'0', u'0', u'1', u'2', u'6'], [u'18', u'\n', u'Celta Vigo', u'0', u'', u'2', u'0', u'0', u'2', u'1', u'3', u'-2', u'', u'0', u'0', u'1', u'0', u'1', u'', u'0', u'0', u'1', u'1', u'2'], [u'19', u'\n', u'Athletic Bilbao', u'0', u'', u'2', u'0', u'0', u'2', u'1', u'3', u'-2', u'', u'0', u'0', u'1', u'0', u'1', u'', u'0', u'0', u'1', u'1', u'2'], [u'20', u'\n', u'Valencia', u'0', u'', u'2', u'0', u'0', u'2', u'2', u'5', u'-3', u'', u'0', u'0', u'1', u'2', u'4', u'', u'0', u'0', u'1', u'0', u'1']]

Now if we are a bit curious (we have to !!!), we realize that there are fields we do not want in our new list of lists and there are strange characters like "u" and "u" 'this is especially important The "u" I think is unicode and the "u" 'with the quotes without anything are fields that do not contain anything, so we do not need them, now we will delete what we do not need with this code:

from prettytable import PrettyTable

tabla_pos = [[u'29 de agosto de 2016Primera Divisi\xf3n de Espa\xf1a 2016/2017 Tabla de posiciones'], [u'\xa0', u'\xa0', u'\xa0', u'General', u'', u'Local', u'', u'Visitante'], [u'Pos.', u'', u'EQUIPO', u'Pts.', u'', u'PJ', u'G', u'E', u'P', u'GF', u'GC', u'Dif.', u'', u'G', u'E', u'P', u'GF', u'GC', u'', u'G', u'E', u'P', u'GF', u'GC'], [u'1', u'\n', u'Las Palmas', u'6', u'', u'2', u'2', u'0', u'0', u'9', u'3', u'6', u'', u'1', u'0', u'0', u'5', u'1', u'', u'1', u'0', u'0', u'4', u'2'], [u'2', u'\n', u'Barcelona', u'6', u'', u'2', u'2', u'0', u'0', u'7', u'2', u'5', u'', u'1', u'0', u'0', u'6', u'2', u'', u'1', u'0', u'0', u'1', u'0'], [u'3', u'\n', u'Real Madrid', u'6', u'', u'2', u'2', u'0', u'0', u'5', u'1', u'4', u'', u'1', u'0', u'0', u'2', u'1', u'', u'1', u'0', u'0', u'3', u'0'], [u'4', u'\n', u'Sevilla FC', u'4', u'', u'2', u'1', u'1', u'0', u'6', u'4', u'2', u'', u'1', u'0', u'0', u'6', u'4', u'', u'0', u'1', u'0', u'0', u'0'], [u'5', u'\n', u'Sporting Gij\xf3n', u'4', u'', u'2', u'1', u'1', u'0', u'2', u'1', u'1', u'', u'1', u'0', u'0', u'2', u'1', u'', u'0', u'1', u'0', u'0', u'0'], [u'6', u'\n', u'Deportivo La Coru\xf1a', u'4', u'', u'2', u'1', u'1', u'0', u'2', u'1', u'1', u'', u'1', u'0', u'0', u'2', u'1', u'', u'0', u'1', u'0', u'0', u'0'], [u'7', u'\n', u'Leganes', u'4', u'', u'2', u'1', u'1', u'0', u'1', u'0', u'1', u'', u'0', u'1', u'0', u'0', u'0', u'', u'1', u'0', u'0', u'1', u'0'], [u'8', u'\n', u'Eibar', u'3', u'', u'2', u'1', u'0', u'1', u'2', u'2', u'0', u'', u'1', u'0', u'0', u'1', u'0', u'', u'0', u'0', u'1', u'1', u'2'], [u'9', u'\n', u'Real Sociedad', u'3', u'', u'2', u'1', u'0', u'1', u'2', u'3', u'-1', u'', u'0', u'0', u'1', u'0', u'3', u'', u'1', u'0', u'0', u'2', u'0'], [u'10', u'\n', u'M\xe1laga', u'2', u'', u'2', u'0', u'2', u'0', u'3', u'3', u'0', u'', u'0', u'1', u'0', u'1', u'1', u'', u'0', u'1', u'0', u'2', u'2'], [u'11', u'\n', u'Atletico Madrid', u'2', u'', u'2', u'0', u'2', u'0', u'1', u'1', u'0', u'', u'0', u'1', u'0', u'1', u'1', u'', u'0', u'1', u'0', u'0', u'0'], [u'12', u'\n', u'Villarreal', u'2', u'', u'2', u'0', u'2', u'0', u'1', u'1', u'0', u'', u'0', u'1', u'0', u'0', u'0', u'', u'0', u'1', u'0', u'1', u'1'], [u'13', u'\n', u'Alav\xe9s', u'2', u'', u'2', u'0', u'2', u'0', u'1', u'1', u'0', u'', u'0', u'1', u'0', u'0', u'0', u'', u'0', u'1', u'0', u'1', u'1'], [u'14', u'\n', u'Espanyol', u'1', u'', u'2', u'0', u'1', u'1', u'6', u'8', u'-2', u'', u'0', u'1', u'0', u'2', u'2', u'', u'0', u'0', u'1', u'4', u'6'], [u'15', u'\n', u'Osasuna', u'1', u'', u'2', u'0', u'1', u'1', u'1', u'3', u'-2', u'', u'0', u'0', u'1', u'0', u'2', u'', u'0', u'1', u'0', u'1', u'1'], [u'16', u'\n', u'Granada', u'1', u'', u'2', u'0', u'1', u'1', u'2', u'6', u'-4', u'', u'0', u'1', u'0', u'1', u'1', u'', u'0', u'0', u'1', u'1', u'5'], [u'17', u'\n', u'Real Betis', u'1', u'', u'2', u'0', u'1', u'1', u'2', u'6', u'-4', u'', u'0', u'1', u'0', u'0', u'0', u'', u'0', u'0', u'1', u'2', u'6'], [u'18', u'\n', u'Celta Vigo', u'0', u'', u'2', u'0', u'0', u'2', u'1', u'3', u'-2', u'', u'0', u'0', u'1', u'0', u'1', u'', u'0', u'0', u'1', u'1', u'2'], [u'19', u'\n', u'Athletic Bilbao', u'0', u'', u'2', u'0', u'0', u'2', u'1', u'3', u'-2', u'', u'0', u'0', u'1', u'0', u'1', u'', u'0', u'0', u'1', u'1', u'2'], [u'20', u'\n', u'Valencia', u'0', u'', u'2', u'0', u'0', u'2', u'2', u'5', u'-3', u'', u'0', u'0', u'1', u'2', u'4', u'', u'0', u'0', u'1', u'0', u'1']]

i = 0

tabla_formateada = []

nueva_form = []

for datos  in tabla_pos:

    if i >= 3:
        while (u'' or u'\n')  in datos:
            if (u'')  in datos :
                datos.remove(u'')
            elif  u'\n'  in datos:
      datos.remove(u'\n')

        tabla_formateada.append(datos)

        nueva_form = tabla_formateada

    i += 1        

print nueva_form

If you look at the code:


We are not doing scraping yet. Simply move the output of the previous program to plain text for quick viewing.


As my Internet is limited I do not want to be down 200 times the same, this output we had now we are going to filter. The first thing we do is initialize the variable i = 0.


We create 2 lists to work on them, and we create a FOR loop with 3 purposes.


The first to remove the 3 initial lists as they contain irrelevant information we do with "if i> = 3:".


The second to remove the blank fields or with medium rare characters like "u '\ n'".


And the third purpose is to create a new formatted list with the data that if we want, this is the output:

[[u'1', u'Las Palmas', u'6', u'2', u'2', u'0', u'0', u'9', u'3', u'6', u'1', u'0', u'0', u'5', u'1', u'1', u'0', u'0', u'4', u'2'], [u'2', u'Barcelona', u'6', u'2', u'2', u'0', u'0', u'7', u'2', u'5', u'1', u'0', u'0', u'6', u'2', u'1', u'0', u'0', u'1', u'0'], [u'3', u'Real Madrid', u'6', u'2', u'2', u'0', u'0', u'5', u'1', u'4', u'1', u'0', u'0', u'2', u'1', u'1', u'0', u'0', u'3', u'0'], [u'4', u'Sevilla FC', u'4', u'2', u'1', u'1', u'0', u'6', u'4', u'2', u'1', u'0', u'0', u'6', u'4', u'0', u'1', u'0', u'0', u'0'], [u'5', u'Sporting Gij\xf3n', u'4', u'2', u'1', u'1', u'0', u'2', u'1', u'1', u'1', u'0', u'0', u'2', u'1', u'0', u'1', u'0', u'0', u'0'], [u'6', u'Deportivo La Coru\xf1a', u'4', u'2', u'1', u'1', u'0', u'2', u'1', u'1', u'1', u'0', u'0', u'2', u'1', u'0', u'1', u'0', u'0', u'0'], [u'7', u'Leganes', u'4', u'2', u'1', u'1', u'0', u'1', u'0', u'1', u'0', u'1', u'0', u'0', u'0', u'1', u'0', u'0', u'1', u'0'], [u'8', u'Eibar', u'3', u'2', u'1', u'0', u'1', u'2', u'2', u'0', u'1', u'0', u'0', u'1', u'0', u'0', u'0', u'1', u'1', u'2'], [u'9', u'Real Sociedad', u'3', u'2', u'1', u'0', u'1', u'2', u'3', u'-1', u'0', u'0', u'1', u'0', u'3', u'1', u'0', u'0', u'2', u'0'], [u'10', u'M\xe1laga', u'2', u'2', u'0', u'2', u'0', u'3', u'3', u'0', u'0', u'1', u'0', u'1', u'1', u'0', u'1', u'0', u'2', u'2'], [u'11', u'Atletico Madrid', u'2', u'2', u'0', u'2', u'0', u'1', u'1', u'0', u'0', u'1', u'0', u'1', u'1', u'0', u'1', u'0', u'0', u'0'], [u'12', u'Villarreal', u'2', u'2', u'0', u'2', u'0', u'1', u'1', u'0', u'0', u'1', u'0', u'0', u'0', u'0', u'1', u'0', u'1', u'1'], [u'13', u'Alav\xe9s', u'2', u'2', u'0', u'2', u'0', u'1', u'1', u'0', u'0', u'1', u'0', u'0', u'0', u'0', u'1', u'0', u'1', u'1'], [u'14', u'Espanyol', u'1', u'2', u'0', u'1', u'1', u'6', u'8', u'-2', u'0', u'1', u'0', u'2', u'2', u'0', u'0', u'1', u'4', u'6'], [u'15', u'Osasuna', u'1', u'2', u'0', u'1', u'1', u'1', u'3', u'-2', u'0', u'0', u'1', u'0', u'2', u'0', u'1', u'0', u'1', u'1'], [u'16', u'Granada', u'1', u'2', u'0', u'1', u'1', u'2', u'6', u'-4', u'0', u'1', u'0', u'1', u'1', u'0', u'0', u'1', u'1', u'5'], [u'17', u'Real Betis', u'1', u'2', u'0', u'1', u'1', u'2', u'6', u'-4', u'0', u'1', u'0', u'0', u'0', u'0', u'0', u'1', u'2', u'6'], [u'18', u'Celta Vigo', u'0', u'2', u'0', u'0', u'2', u'1', u'3', u'-2', u'0', u'0', u'1', u'0', u'1', u'0', u'0', u'1', u'1', u'2'], [u'19', u'Athletic Bilbao', u'0', u'2', u'0', u'0', u'2', u'1', u'3', u'-2', u'0', u'0', u'1', u'0', u'1', u'0', u'0', u'1', u'1', u'2'], [u'20', u'Valencia', u'0', u'2', u'0', u'0', u'2', u'2', u'5', u'-3', u'0', u'0', u'1', u'2', u'4', u'0', u'0', u'1', u'0', u'1']]

Now to finish this entry, in the next we will pick up interesting things from this same project, we will show the data in a table, but not all data only 4, this is the code.

numero_items = len(nueva_form[0])
print numero_items
table = PrettyTable(["Posicion", "Equipo", "Puntos", "Dif_Gol"])
i = 0
for i in range(0,numero_items):
    if i >= 0:
        table.add_row([nueva_form[i][0], nueva_form[i][1], nueva_form[i][2] , nueva_form[i][9]])
    i += 0
print table


Remember to link this code to the previous one, The output of this Code Piece is this:

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

The complete and working code of the program we have done is this:

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

# url that we are scraping
url = "http://www.espn.com.ve/futbol/posicionesenvivo/_/liga/esp.1/primera-division-de-espana"

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_pos = results

i = 0

tabla_formateada = []

nueva_form = []

for datos  in tabla_pos:

    if i >= 3:
        while (u'' or u'\n')  in datos:
            if (u'')  in datos :
                datos.remove(u'')
            elif  u'\n'  in datos:
      datos.remove(u'\n')

        tabla_formateada.append(datos)

        nueva_form = tabla_formateada

    i += 1        

numero_items = len(nueva_form[0])

table = PrettyTable(["Posicion", "Equipo", "Puntos", "Dif_Gol"])
i = 0
for i in range(0,numero_items):
    if i >= 0:
        table.add_row([nueva_form[i][0], nueva_form[i][1], nueva_form[i][2] , nueva_form[i][9]])

    i += 0

print table

Thanks until the next God Bless you, until the next: D

Coin Marketplace

STEEM 0.19
TRX 0.13
JST 0.029
BTC 60723.46
ETH 3353.68
USDT 1.00
SBD 2.51