mirror of
https://github.com/nprasad2077/nbaStats.git
synced 2026-11-14 13:53:47 +00:00
JSON scraper works
This commit is contained in:
1 parent
d514581366
commit
9d89922697
1 file changed
+21
-10
+21
-10
@@ -1,6 +1,7 @@
|
|||||||
import requests
|
import requests
|
||||||
from bs4 import BeautifulSoup
|
from bs4 import BeautifulSoup
|
||||||
import csv
|
import csv
|
||||||
|
import json
|
||||||
import os
|
import os
|
||||||
|
|
||||||
url = 'https://www.basketball-reference.com/teams/HOU/2018.html'
|
url = 'https://www.basketball-reference.com/teams/HOU/2018.html'
|
||||||
@@ -16,20 +17,30 @@ rows = table.find("tbody").find_all("tr")
|
|||||||
# Extract coloumn headers
|
# Extract coloumn headers
|
||||||
headers = [col.text.strip() for col in header_row.find_all("th")][1:]
|
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.
|
|
||||||
|
|
||||||
|
# extract rows as dictionaries
|
||||||
|
data = []
|
||||||
for row in rows:
|
for row in rows:
|
||||||
cols = row.find_all("td")
|
cols = row.find_all("td")
|
||||||
cols = [col.text.strip() for col in cols]
|
cols = [col.text.strip() for col in cols]
|
||||||
data.append(cols)
|
row_dict = dict(zip(headers, cols))
|
||||||
|
data.append(row_dict)
|
||||||
|
|
||||||
|
## locate current directory
|
||||||
script_dir = os.path.dirname(os.path.abspath(__file__))
|
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:
|
## CSV file
|
||||||
writer = csv.writer(csvfile)
|
|
||||||
writer.writerows(data)
|
csv_output_path = os.path.join(script_dir, '..', 'data', 'output.csv')
|
||||||
|
|
||||||
|
with open(csv_output_path, "w", newline="") as csvfile:
|
||||||
|
writer = csv.DictWriter(csvfile, fieldnames =headers)
|
||||||
|
writer.writeheader()
|
||||||
|
for row in data:
|
||||||
|
writer.writerow(row)
|
||||||
|
|
||||||
|
# Save as JSON
|
||||||
|
|
||||||
|
json_output_path = os.path.join(script_dir, '..', 'data', 'output.json')
|
||||||
|
with open(json_output_path, 'w') as jsonfile:
|
||||||
|
json.dump(data, jsonfile)
|
||||||
Reference in new issue
Block a user