Last active
September 24, 2015 01:34
-
-
Save yonghanjung/ec149476f743266bc1bb 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
# -*- coding: utf-8 -*- | |
__author__ = 'jeong-yonghan' | |
import csv | |
import numpy as np | |
import urllib | |
import re | |
import time | |
HoonsName = "Loc.txt" | |
Hoons = open(HoonsName,'rb') | |
LocList = list() | |
for row in Hoons: | |
x = float(row.split('\t')[0]) | |
y = row.split('\t')[1] | |
y = y.replace('\t','') | |
y = float(y) | |
LocList.append([x,y]) | |
# HoonsURL = "http://data.fcc.gov/api/block/2010/find?latitude=37.76101609&longitude=-122.4193502" | |
Hoons_HostURL = "http://data.fcc.gov/api/block/2010/find?" | |
ResultFile = open('HoonsResult.txt','w') | |
for idx in range(len(LocList)): | |
try: | |
time.sleep(0.1) | |
HoonsLoc = LocList[idx] | |
HoonsURL = Hoons_HostURL + 'latitude=' + str(HoonsLoc[1]) + '&longitude=' + str(HoonsLoc[0]) | |
HTML = urllib.urlopen(HoonsURL).read() | |
# print HTML | |
regex = "<Block FIPS=(.+?)/>" | |
pattern = re.compile(regex) | |
result = re.findall(pattern, HTML) | |
Code = result[0].replace('"','') | |
ResultFile.write(Code + str('\n')) | |
print "Success at", HoonsLoc, 'with', Code | |
# if idx > 10: | |
# break | |
except: | |
ErrorLog = "ERROR at latitude=" + str(HoonsLoc[1]) + ' & longitude='+str(HoonsLoc[0]) | |
print ErrorLog | |
ResultFile.write(ErrorLog+str('\n')) | |
ResultFile.close() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment