Skip to content

Instantly share code, notes, and snippets.

@theriley106
Created December 12, 2017 18:53
Show Gist options
  • Save theriley106/a25f2eff3f90297d5b4b61ed86a08843 to your computer and use it in GitHub Desktop.
Save theriley106/a25f2eff3f90297d5b4b61ed86a08843 to your computer and use it in GitHub Desktop.
Net Neutrality Voting
import bs4
import requests
import csv
import json
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'}
res = requests.get('https://www.theverge.com/2017/12/11/16746230/net-neutrality-fcc-isp-congress-campaign-contribution', headers=headers)
page = bs4.BeautifulSoup(res.text, 'lxml')
dataBase = []
with open('example.csv', 'rb') as f:
reader = csv.reader(f)
data = list(reader)
for var in data:
dataBase.append(str(var))
def extractInfo(listItem):
vote = 'Unknown'
if 'yea' in str(listItem).lower():
vote = "Yes"
if 'nay' in str(listItem).lower():
vote = "No"
state = str(listItem).partition("', '")[2].partition("'")[0]
return [state, vote]
information = {}
for i, lpage in enumerate(page.select('table')):
for member in lpage.select('tr'):
try:
column = member.select('td')
if 'Member Name' not in str(column):
vote = 'Unknown'
name = ' '.join(str(column[0].getText()).split(", ")[::-1])
if str(name).count(' ') == 2:
firstName = name.split(' ')[0]
lastName = name.split(' ')[2]
name = ' '.join([firstName, lastName])
if i == 0:
party = column[2].getText()
else:
party = column[1].getText()
found = False
for value in dataBase:
if name in str(value):
found = True
state, vote = extractInfo(value)
if found == False:
for value in dataBase:
if name.partition(' ')[2] in str(value):
found = True
state, vote = extractInfo(value)
if found == False:
if i == 0:
state = column[1].getText()
else:
state = str(column[2].getText())[:2]
if party.lower() == 'd':
party = "Democrat"
if party.lower() == 'r':
party = "Republican"
if i == 0:
#this indicates it's a state representative
information[name] = {"Position": "US Senate", "State": state, "Party": party, "Contributions": column[3].getText(), "Vote": vote}
else:
#this means it's a house of representative
information[name] = {"Position": "House of Representatives", "State": state, "Party": party, "Contributions": column[3].getText(), "Vote": vote}
except Exception as exp:
print exp
pass
with open('result.json', 'w') as fp:
json.dump(information, fp)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment