# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + [markdown] id="view-in-github" colab_type="text"
# <a href="https://colab.research.google.com/github/k-avya/master/blob/master/WEB_SCRAPPING.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>

# + [markdown] id="MD9uogrOQrsU"
# # I have done web scraping of IPL player runs stats data of 2020 and perform a quick data exploration. 
#
#
#

# + id="fMLOkhDzKup2"
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
# %matplotlib inline
import warnings
warnings.filterwarnings("ignore")
import pandas.util.testing as tm

# + id="U8gNFvBxgNnE" outputId="7d29df6a-0e92-4fd7-cbdd-3780acbc4d44" colab={"base_uri": "https://localhost:8080/", "height": 35}
# The Building Blocks
year = '2020'
str = 'https://www.iplt20.com/stats/2020/most-runs'

# Combining the URL + year strings together
url = str.format(year)
url

# + id="-OGK2TyCOpO1" outputId="de221651-4de2-4c7b-a56b-f03a3928ab57" colab={"base_uri": "https://localhost:8080/", "height": 104}
years = [2016,2017,2018,2019,2020]
str = 'https://www.iplt20.com/stats/2020/most-runs'

for year in years:
  url = str.format(year)
  print(url)

# + [markdown] id="1uXvGYOuRYxX"
# **Read HTML webpage into pandas**

# + id="N835lCmOPkgO"
import pandas as pd

# + id="kPavoFHyPs5V" outputId="2e4db2cc-3487-4b27-ed6a-e12b26ff4828" colab={"base_uri": "https://localhost:8080/", "height": 260}
df = pd.read_html(url, header = 0)
df

# + [markdown] id="pIASc-kDRp85"
# How many tables are there in the webpage?

# + id="LUvI6EfsPyv5" outputId="561eb82a-34ed-4b19-a4f9-5dff6b8f7b27" colab={"base_uri": "https://localhost:8080/", "height": 35}
len(df)

# + id="gmemrlhbQEWY" outputId="5822ed2e-5d54-4f11-e420-353f5f39b7b4" colab={"base_uri": "https://localhost:8080/", "height": 415}
df[0]

# + id="XDol_KhKQKM1"
df2020 = df[0]

# + [markdown] id="mJXqRaLDTU6i"
# **Quick Exploratory Data Analysis**

# + id="V2drGZPdTamk"
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
# %matplotlib inline
import re
import time
from datetime import datetime
import matplotlib.dates as mdates
import matplotlib.ticker as ticker
from urllib.request import urlopen
from bs4 import BeautifulSoup
import requests


# + id="Aw2nSU69pfTb"
from bokeh.models import ColumnDataSource
from bokeh.transform import dodge
import math
from bokeh.io import curdoc
curdoc().clear()
from bokeh.io import push_notebook, show, output_notebook
from bokeh.layouts import row
from bokeh.plotting import figure
from bokeh.transform import factor_cmap
from bokeh.models import Legend
output_notebook()

# + id="n8JEwolkuxFp"
data = df2020.sort_values(["Runs"], axis=0, ascending=False)[:80]

# + id="ZcD_N1IGv_CE" outputId="3bdbddb8-f199-4262-f042-071217bfc52d" colab={"base_uri": "https://localhost:8080/", "height": 415}
data

# + id="Let6H3suuE36"
p = figure(x_range=data.iloc[:,1], plot_width=800, plot_height=550, title="Highest Run Scorer", toolbar_location=None, tools="")

p.vbar(x=data.iloc[:,1], top=data.iloc[:,4], width=0.9)

p.xgrid.grid_line_color = None
p.y_range.start = 0
p.xaxis.major_label_orientation = math.pi/2

# + id="_hYMY30Auh18" outputId="72f0afa1-be06-4074-a94a-edff387a16bc" colab={"base_uri": "https://localhost:8080/", "height": 567}
show(p)

# + id="X6BA6PTIvoLl" outputId="982d4947-28cd-444e-acf1-c0002845cbbb" colab={"base_uri": "https://localhost:8080/", "height": 297}
sns.distplot(df2020.Runs,
             kde=False,
             hist_kws=dict(edgecolor="black", linewidth=2),
             color='#00BFC4')

# + id="7GALFy3qw3er"
data = df2020[df2020['Runs'] > 40]

# + id="Mcv5Gz0Cw_0A"
data = data.sort_values(['Runs'],axis=0, ascending=False)[:15]

# + id="NVhdMCkIxGKg" outputId="a9a05aa9-5a8f-46ee-d323-837d451a3ac5" colab={"base_uri": "https://localhost:8080/", "height": 509}
data

# + id="zgaDISnGyTHu"
p = figure(x_range=data.iloc[:,1], plot_width=800, plot_height=600, title="Top Rated Books with more than 1000 Customers Rating", toolbar_location=None, tools="")

p.vbar(x=data.iloc[:,1], top=data.iloc[:,2], width=0.9)

p.xgrid.grid_line_color = None
p.y_range.start = 0
p.xaxis.major_label_orientation = math.pi/2

# + id="ZymbHRJzyVrj" outputId="a5fb98d8-6613-414e-e6aa-add6dd65f8e9" colab={"base_uri": "https://localhost:8080/", "height": 617}
show(p)

# + id="SVkQW_SQyasE"
data = df2020.sort_values(["Runs"], axis=0, ascending=False)[:20]

# + id="h1c6TGDNyxQ2" outputId="22d8f6f2-e8d3-4ce3-<PASSWORD>" colab={"base_uri": "https://localhost:8080/", "height": 662}
data

StarcoderdataJupyterScriptsDedupFiltered