-1

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
Community
  • 1
  • 1
Daniel Royer
  • 75
  • 1
  • 11
  • 3
    XPath's are defined over XML, not JSON. Use a JSON library instead. – kjhughes Jun 01 '16 at 20:49
  • Can I use a JSON library with Python script though? – Daniel Royer Jun 01 '16 at 21:04
  • Certainly: See **(a)** [Reading a JSON file using Python](http://stackoverflow.com/questions/20199126/reading-a-json-file-using-python), and **(b)** [Parsing values from a JSON file in Python](http://stackoverflow.com/questions/2835559/parsing-values-from-a-json-file-in-python) – kjhughes Jun 01 '16 at 21:26

1 Answers1

1

If you meant to post this (http://maps.googleapis.com/maps/api/geocode/xml?address=27517) address instead of the one you linked, the XPath would be:

/GeocodeResponse/result/geometry/location/lat

and

/GeocodeResponse/result/geometry/location/lng

If you want to do it in JSON, it's different.

Dan Field
  • 20,885
  • 5
  • 55
  • 71
  • Thanks. I inputted that into my spider and run "scrapy crawl latlng -o coords.csv -t csv" in my prompt which would save the yielded coordinates into a csv file. However the csv is saved but is empty, and I don't know what is wrong. – Daniel Royer Jun 02 '16 at 01:32