mirror of
https://github.com/nprasad2077/nbaStats.git
synced 2026-11-17 15:23:47 +00:00
scrapping success :D
This commit is contained in:
1 parent
6ecf1ddddb
commit
d514581366
1 file changed
+24
-2
+24
-2
@@ -1,5 +1,7 @@
|
|||||||
import requests
|
import requests
|
||||||
from bs4 import BeautifulSoup
|
from bs4 import BeautifulSoup
|
||||||
|
import csv
|
||||||
|
import os
|
||||||
|
|
||||||
url = 'https://www.basketball-reference.com/teams/HOU/2018.html'
|
url = 'https://www.basketball-reference.com/teams/HOU/2018.html'
|
||||||
response = requests.get(url)
|
response = requests.get(url)
|
||||||
@@ -7,7 +9,27 @@ response = requests.get(url)
|
|||||||
soup = BeautifulSoup(response.text, "html.parser")
|
soup = BeautifulSoup(response.text, "html.parser")
|
||||||
|
|
||||||
table = soup.find("table", {"id": "per_game"})
|
table = soup.find("table", {"id": "per_game"})
|
||||||
rows = table.find_all("tr")
|
# rows = table.find_all("tr")
|
||||||
|
header_row = table.find("thead").find("tr")
|
||||||
|
rows = table.find("tbody").find_all("tr")
|
||||||
|
|
||||||
|
# Extract coloumn headers
|
||||||
|
headers = [col.text.strip() for col in header_row.find_all("th")][1:]
|
||||||
|
|
||||||
|
# add coloumn headers to the data list
|
||||||
|
data = [headers]
|
||||||
|
|
||||||
|
# extract rows of data into proper column with headers.
|
||||||
|
|
||||||
|
for row in rows:
|
||||||
|
cols = row.find_all("td")
|
||||||
|
cols = [col.text.strip() for col in cols]
|
||||||
|
data.append(cols)
|
||||||
|
|
||||||
|
|
||||||
# print(rows)
|
script_dir = os.path.dirname(os.path.abspath(__file__))
|
||||||
|
output_path = os.path.join(script_dir, '..', 'data', 'output.csv')
|
||||||
|
|
||||||
|
with open(output_path, "w", newline="") as csvfile:
|
||||||
|
writer = csv.writer(csvfile)
|
||||||
|
writer.writerows(data)
|
||||||
Reference in new issue
Block a user