-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1-us-house-scrape-data.py
More file actions
31 lines (26 loc) · 1 KB
/
Copy path1-us-house-scrape-data.py
File metadata and controls
31 lines (26 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import pandas as pd
import requests
from tqdm import tqdm
# DATA SCRAPING - US HOUSE OF REPRESENTATIVES
s = requests.Session()
dataframes = []
for year in tqdm(range(1990, 2024), desc='Year'):
exceptions = 0
for bill in tqdm(range(1, 1000), desc='Bill', leave=False):
url = f"https://clerk.house.gov/Votes/{year}{bill}"
try:
table = pd.read_html(url, attrs={"class": "allvotes-table"})[0]
table.columns = ["Person", "Person", "Party", "State", "State_short", "Vote"]
table["year"] = year
table["bill"] = bill
dataframes.append(table)
except Exception as e:
print(f"Error at year {year}, bill {bill}. Url {url}")
print(e)
exceptions += 1
if exceptions > 10:
break
# Write all data to file
with open("./data/us-house/real-data/clerk_data.tsv.gz", "w+") as f:
for df in dataframes:
df.to_csv(f, sep="\t", index=None, header=None,compression="gzip")