Giter VIP home page Giter VIP logo

afc-stat-scraping-project-2019's Introduction

AFC Stat Scraping Project 2019

  1. We need to setup an environment to do our scraping, start off on the Desktop
mkdir scraping00
cd scraping00
  1. Create a virtual Environment within the directory and then activate
virtualenv - p python3 .
soucre bin/activate
  1. create an src directory to store the project
mkdir src
cd src
  1. Download/pip install the dependacies for our projects
pip install requests
pip install BeautifulSoup
pip install jupyter
pip install pandas
  1. lets open up Jupyter Notebook, a window with Jupyter notebooks
jupyter notebook
  1. click on New, Python 3, will give us a brand new notebook for our scraping project

  2. lets start off with our imports

import sys
import collection as co
from requests import *
from bs4 import BeautifulSoup
import pandas as pd
  1. lets get the url, response status code
url = "https://www.pro-football-reference.com/years/2018/index.htm"

response = get(url)

print(response.status_code)
  1. We will create a BeautifulSoup object and pass it through a variable
nfl = BeautifulSoup(response.content, 'html.parser)
  1. Now we search for the element that we are looking
afc_table = nfl.find('div',{'class':'overthrow table_container'})
  1. We are going to grab headers
table_head = afc_table.find('thead')
header = []
for th in table_head.findAll('th):
    key = th.get_text()
    header.append(key)
print(header)
  1. We are going to count the rows
endrows = 0
for tr in afc_table.findAll('tbody'):
    if tr.findAll('th')[0].get_text() in (''):
        endrows += 1

rows = len(afc_table.findAll('tr'))
rows -= endrows + 1

print(rows)
  1. We are going to add it to a Pnadas DataFrame
list_of_dicts = []
for row in range(rows):
    the_row = []
    try:
        table_row = afc_table.findAll('tr')[row]
        for tr in table_row:
            value = tr.get_text()
            the_row.append(value)
        od = co.OrderedDict(zip(header,the_row))
        list_of_dicts.append(od)
    except AttributeError:
        continue

df = pd.DataFrame(list_of_dicts)
df
  1. Clean the data, remove the NaN
df = df[:].fillna(")
  1. Let's save it to a csv file
df.to_csv("afc_2018_stat.csv")

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.