DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Coding
  3. Languages
  4. Using Python to fetch Republican Primary results in JSON

Using Python to fetch Republican Primary results in JSON

Chris Smith user avatar by
Chris Smith
·
Mar. 08, 12 · Interview
Like (0)
Save
Tweet
Share
6.17K Views

Join the DZone community and get the full member experience.

Join For Free
Over at Pastie.org, they have an interesting Python Script that returns the results of the Republican Primary in JSON.
import json
import sys
import urllib2
from BeautifulSoup import BeautifulSoup
import re


#Usage python elections.py State Name
#Example python elections.py massachusetts

state = sys.argv[1]
url = ("http://elections.msnbc.msn.com/ns/politics/2012/%s/Republican/primary/" % state)
html = BeautifulSoup(urllib2.urlopen(url)).prettify()

soup = BeautifulSoup(html)


cities = soup.findAll("div", {"class": re.compile(r'\bresultSlice\ countyResults\b')})

results = {}

for city in cities:
    cityName = ''.join((city.find("div", {"class": "state"})).findAll(text=True)).strip()
    results.setdefault(cityName, {})

    table = city.findAll("table", {"class": "results"})
    for rows in table:
        rows = rows.findAll("tr", {"class": re.compile(r'\brep\b')})
        for row in rows:
            try:
                canidate    = ''.join(row.find("td", {"class": "name"}).findAll(text=True)).strip()
                votes       = ''.join(row.find("td", {"class": "votes"}).findAll(text=True)).strip()
                if votes == "":
                    votes = 0
                results.setdefault(cityName, {}).setdefault((canidate.replace("\n","")).replace("                            ", " "), {})['votes'] = votes
            except Exception:
                continue

print json.dumps(results, indent=4)

Enjoy!
JSON Python (language) Fetch (FTP client)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • 10 Most Popular Frameworks for Building RESTful APIs
  • Software Maintenance Models
  • LazyPredict: A Utilitarian Python Library to Shortlist the Best ML Models for a Given Use Case
  • Metrics Part 2: The DORA Keys

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: