I was confused on how to get the latitude and longitude values under "Location" at this URL using two separate xpath's.
I am using this to extract these two values for the coordinates of a given zip code. However the coords.csv file is empty when I run "scrapy crawl latlng -o coords.csv -t csv" so the xpath or method I have yielding coords must not be correct
Here is my code:
Use GoogleMaps API to find location for a given zip and extract the latitude(lat) & longitude(lng) with 'latlng' spider
zipcode = raw_input('Zipcode: ')
latlngurl = 'http://maps.googleapis.com/maps/api/geocode/json?address=%s' % (zipcode,)
latitude = 35
longitude = -79
class Coordinates(scrapy.Item):
Latitude = scrapy.Field()
Longitude = scrapy.Field()
class LatlngSpider(scrapy.Spider):
name = "latlng"
allowed_domains = ["googleapis.com"]
start_urls = (
latlngurl,
)
def parse(self, response):
latitude = response.xpath('/GeocodeResponse/result/geometry/location/lng').extract()
longitude = response.xpath('/GeocodeResponse/result/geometry/location/lat').extract()
for element in range(0, 2, 1):
coords = Coordinates()
coords["Latitude"] = latitude.pop(0)
coords["Longitude"] = longitude.pop(0)
yield coords