Created
November 8, 2018 20:44
-
-
Save stucka/6a6295e8e0da7acbec7f673848391bd0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 19, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"from pyquery import PyQuery as pq\n", | |
"import requests\n", | |
"\n", | |
"import csv\n", | |
"from collections import OrderedDict" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"localurl = \"https://enr.electionsfl.org/BRO/1985/Precincts/21073/?view=detailed\"" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"html = requests.get(localurl).content" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 33, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Parsing United States Senator\n" | |
] | |
} | |
], | |
"source": [ | |
"masterlist = []\n", | |
"options = pq(pq(html)(\"select#raceSelect\"))(\"option\")\n", | |
"for option in options:\n", | |
" if pq(option).attr(\"selected\"): # if not a none value\n", | |
" racename = pq(option).text()\n", | |
" print(\"Parsing \" + racename)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 34, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"for racerow in pq(html)(\"div.Race.row\"):\n", | |
" precinctname = pq(racerow)(\"span.PrecinctName\").text().strip()\n", | |
" # print(precinctname)\n", | |
" table = pq(racerow)(\"table.DetailResults\")\n", | |
" headers = []\n", | |
" for th in pq(pq(pq(table)(\"tr\"))[0])(\"th\"):\n", | |
" headers.append(pq(th).text())\n", | |
" for row in pq(pq(table)(\"tr\"))[1:]:\n", | |
" line = OrderedDict()\n", | |
" line['racename'] = racename\n", | |
" line['precinctname'] = precinctname\n", | |
" for i, item in enumerate(headers):\n", | |
" stuff = pq(pq(pq(row)(\"td\"))[i]).text().strip().replace(\"\\r\\n\", \"\")\n", | |
" line[item] = stuff\n", | |
" masterlist.append(line)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 35, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"with open(\"masterlist.csv\", \"w\", newline=\"\") as f:\n", | |
" writer = csv.writer(f)\n", | |
" writer.writerow(list(line.keys()))\n", | |
" for row in masterlist:\n", | |
" writer.writerow(list(row.values()))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.6.3" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment